When the Integers got upset!
Practice
4.1 (68 votes)
Bit manipulation
Bit manipulation
Dynamic programming
Medium
Open
Problem
91% Success 2285 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Puchi was crossing a river by jumping over stepping stones, carrying a bag containing N integers. Our Monk saw him from the banks and shouted out to him. Shaken by the sudden shout, Puchi dropped the bag in the river and all the integers got wet! This upset the integers.
There are N stepping stones available ahead. Puchi wants to dry these integers on these stones, such that each stone contains a single integer. The upset value of the i'th integer Ai (after rearrangement of the integers) is (Ai ^ Ai-1 ^ Ai-2) times Pi for i ≥ 2, and 0 otherwise. (where '^' is the bit wise XOR operator). Here, i represents the position of the said integer.

Puchi asks our Monk to find an order of arranging the numbers on the stones, such that sum of upset values is minimized. Help our Monk find the order, given the arrays A and P .
Input:
First line contains T. T test cases follow.
First line of each test case contains an integer N.
Second line of each test case contains N integers, forming the array A.
Third line of each test case contains N integers, forming the array P.

Output:
Print the answer to each test case in a new line.

Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 12
0 ≤ Ai ,Pi ≤ 200

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
3 votes
Tags:
AlgorithmsDynamic ProgrammingMedium
Points:30
2 votes
Tags:
Breadth-first searchAlgorithmsDynamic Programming and Bit MaskingBrute-force searchStringDynamic ProgrammingBitmask
Points:30
14 votes
Tags:
AlgorithmsBit ManipulationDynamic ProgrammingMedium