#P15458. 【MX-X25-T2】『FeOI-5』2 的次幂

【MX-X25-T2】『FeOI-5』2 的次幂

Problem Description

You are given a non-negative integer sequence aa of length nn. You may perform the following operation any number of times:

  1. Choose an interval [l,r][l,r] (where ll and rr may be equal).
  2. Let s=∑i∈[l,r]ais=\sum \limits_{i\in [l,r]} a_i, and let kk be the largest non-negative integer such that 2k≤s2^k\le s (if s=0s=0 then k=0k=0).
  3. You gain kk coins, and delete a[l,r]a_{[l,r]}. After that, a[1,l−1]a_{[1,l-1]} and a[r+1,n]a_{[r+1,n]} are concatenated, i.e., the new length of aa becomes n−r+l−1n-r+l-1.

Find the maximum number of coins you can obtain.

Input Format

The first line contains two integers c,Tc,T, representing the subtask ID of the test point (the samples guarantee c=0c=0) and the number of test cases.

For each test case, the input contains two lines:

  • The first line contains an integer nn.
  • The second line contains nn integers representing the sequence aa.

Output Format

For each test case, output one line with one integer representing the answer.

0 1
5
3 2 1 1 4
5
0 1
12
1 0 3 2 2 1 4 5 6 8 7 9
19

Hint

Sample 1 Explanation

First operation: choose interval [2,2][2,2]. Then s=2,k=1s=2,k=1, you gain 11 coin, and the sequence becomes 3 1 1 4 after the operation.

Second operation: choose interval [1,3][1,3]. Then s=5,k=2s=5,k=2, you gain 22 coins, and the sequence becomes 4 after the operation.

Third operation: choose interval [1,1][1,1]. Then s=4,k=2s=4,k=2, you gain 22 coins, and the sequence becomes an empty sequence after the operation.

In total you gain 55 coins. It can be proven that this is the maximum number of coins that can be obtained.

Constraints

For all testdata, 1≤n,∑n≤1061\le n,\sum n\le 10^6, 0≤ai≤10180\le a_i\le 10^{18}, 1≤T≤1001\le T\le 100.

Subtask ID ∑n\sum n Special Property Score
11 ≤5\le 5 None 55
22 ≤100\le 100 1010
33 ≤106\le 10^6 0≤ai≤30\le a_i\le 3
44 ∀i\forall i,there does not exist kk such that ai=2k−1a_i=2^k-1 2020
55 ≤5000\le 5000 None
66 ≤106\le 10^6 3535

Translated by ChatGPT 5