#P16279. 「MierOI R1」Future

    ID: 17705 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>线段树洛谷原创O2优化优先队列洛谷月赛反悔贪心

「MierOI R1」Future

Problem Description

Given a kk-digit decimal number N=n1n2nkN=\overline{n_1n_2\dots n_k}. It is guaranteed that k\bm k is even, and ni0\bm{n_i \ne 0}.

You can perform the following operation exactly k2\frac{k}{2} times:

  • Delete any two adjacent digits ni,ni+1n_i,n_{i+1} in NN, and gain nini+1\overline{n_in_{i+1}} points. The remaining digits are automatically concatenated.

Find the maximum total score.

Input Format

This problem contains multiple test cases.

The first line of the input contains a positive integer TT, indicating the number of test cases.

Then, the TT test cases follow sequentially. For each test case:

  • The first line contains a single positive integer kk.
  • The second line contains a kk-digit decimal number NN.

Output Format

For each test case, output a single line containing an integer, representing the maximum total score.

3
4
2432
8
19919911
16
1991991119919911
65
292
656

Hint

Explanation for Sample #1

For the first test case, the optimal sequence of operations is as follows:

  • Delete digits n2=4,n3=3n_2=4,n_3=3, gaining 4343 points. NN becomes 2222.
  • Delete digits n1=2,n2=2n_1=2,n_2=2, gaining 2222 points. NN becomes empty.

The total score is 43+22=6543+22=65 points. It can be proven that this is the maximum total score.

Data Range

This problem uses bundled subtasks.

For all test cases, it is guaranteed that 1T51 \le T \le 5, 2k1052 \le k \le 10^5, and 1ni91 \le n_i \le 9.

::cute-table{tuack}

Subtask kk \le Special Property Score
11 1010 None 2020
22 10310^3 ^
33 10510^5 A
44 ^ None 4040
  • Special Property A: ni2n_i \le 2.