give cpp answer for the following question : Buying Order (Easy) This is the easy…
- User
give cpp answer for the following question : Buying Order (Easy) This is the easy version of the problem available for 100 points in all divisions. Note that here you need to find the maximum number of items possible.
There's a shop with N N distinct items numbered 1 1, 2 2, .... N N. There's also a boolean array B B of length N N.
You want to buy all N N items once each, in some order. Say this order is P 1 , P 2 , . . . , P N P 1 ,P 2 ,...,P N . When buying item P i P i ,
If B P i
1 B P i
=1 and there exists some j < i j<i such that P j
P i P j
P i , you get two copies of item P i P i , otherwise you get one copy. Informally, you get two copies if you have bought an item with a larger index before, otherwise you get one copy.
If B P i
0 B P i
=0, it's the opposite - if there exists some j < i j<i such that P j < P i P j
Find the maximum total number of items that you can have in the end.
Input Format The first line of input will contain a single integer T T, denoting the number of test cases. Each test case consists of multiple lines of input. The first line of each test case contains N N - the number of items The second line contains N N boolean variables - B 1 , B 2 , . . . , B N B 1 ,B 2 ,...,B N . Output Format For each test case, output on a new the maximum possible number of items.
Constraints 1 ≤ T ≤ 1 0 4 1≤T≤10 4
2 ≤ N ≤ 2 ⋅ 1 0 5 2≤N≤2⋅10 5
B i
0 B i =0 or B i
1 B i =1 The sum of N N does not exceed 2 ⋅ 1 0 5 2⋅10 5 . Sample 1: Input Output 2 2 0 1 2 1 1 2 3 Explanation: Test Case 1 : It can be proven that regardless of what order we buy the items in, we will always get 1 1 of each.
Test Case 2 : Buying the items in order P
[ 2 , 1 ] P=[2,1] is optimal, as you get 1 1 copy of item 2 2 and 2 2 copies of item 1 1.
- Assistant