Reduce the Array
Practice
3.8 (4 votes)
Bitmask
Bit manipulation
Algorithms
Minimum spanning tree
Graphs
Disjoint set union
Minimum spanning tree
Problem
87% Success 639 Attempts 30 Points 5s Time Limit 256MB Memory 1024 KB Max Code

You are given an array of \(N\) numbers, and you are asked to minimize the total "\(OR \ \ cost\)" incurred when reducing the array to a single number. To achieve this, you need to follow these steps:

  1. Select any two numbers \(P\) and \(Q\) from the array. The cost of selecting these two numbers is \(((P + Q) + (P | Q) - (P & Q))\), where "|" denotes the bitwise \(OR\) operator, and "&" denotes the bitwise \(AND\) operator.
  2. Choose one of the two numbers you selected in the previous step and remove it from the array. Concatenate the remaining numbers.

  3. Repeat the above two steps until the array is reduced to a single number.

The "\(OR\) \(cost\)" is defined as the bitwise \(OR\) of all the costs incurred in reducing the array to a single number.

 

Input Format :

An integer \(N\) denoting the size of the array

\(N\) integers denoting the elements of the array

Output Format :

Print one integer, the minimum \(OR \ \ cost\)

Constraints :

\(1 \leq N \leq 10^3\)

\(1 \leq a_i \leq 10^6\)

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
5 votes
Tags:
AlgorithmsApprovedGraphsMediumMinimum spanning treeOpen
Points:30
18 votes
Tags:
AlgorithmsApprovedGraphsGreedy AlgorithmsMediumMinimum spanning treeOpenSets
Points:30
15 votes
Tags:
Breadth First SearchDepth First SearchGraphsMediumMinimum Spanning Tree