A good array
Practice
4 (9 votes)
Greedy algorithms
Algorithms
Basics of greedy algorithms
Greedy algorithm
Problem
53% Success 2282 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an array $$A$$ which consists of $$N$$ elements. Each element of array is either zero or one. Your task is to convert the given array into a good array with a minimum cost.

Good array: In a good array, select any subarray of even length $$M$$ and the sum of elements in the subarray will be $$M/2$$.

In order to transform an array to good array, you can perform the following two operations as many times as you want:

  1.  Choose any index $$1≤i≤N$$ and if it is currently 0, then convert it to 1 for X coins.
  2.  Choose any index $$1≤i≤N$$ and if it is currently 1, then convert it to 0 for Y coins.

Your task is to minimize the cost of transformation of an array to good array.

Input format

  • The first line contains $$T$$ denoting the number of test cases.
  • The first line of each test case contains three space-separated integers $$N$$, $$X$$, and $$Y$$.
  • The second line of each test case consists of $$N$$ space-separated integers of array $$A$$.

Output format

For each test case, print one line denoting the minimum cost to transform array $$A$$ into a good array.

Constraints

\(1 \leq T \leq 100\)

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

\(0 \leq A_i \leq 1\)

\(1 \leq X,Y \leq 10^9\)

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:20
13 votes
Tags:
AlgorithmsArraysBrute-force searchEasyGreedy Algorithms
Points:20
30 votes
Tags:
EasyGreedy Algorithms
Points:20
20 votes
Tags:
AlgorithmsEasyGreedy AlgorithmsSortingapprovedhiring