CF1637B.MEX and Array

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Let there be an array b1,b2,,bkb_1, b_2, \ldots, b_k . Let there be a partition of this array into segments [l1;r1],[l2;r2],,[lc;rc][l_1; r_1], [l_2; r_2], \ldots, [l_c; r_c] , where l1=1l_1 = 1 , rc=kr_c = k , and for any 2ic2 \leq i \leq c holds that ri1+1=lir_{i-1} + 1 = l_i . In other words, each element of the array belongs to exactly one segment.

Let's define the cost of a partition as c+i=1cmex({bli,bli+1,,bri}),c + \sum_{i = 1}^{c} \operatorname{mex}(\{b_{l_i}, b_{l_i + 1}, \ldots, b_{r_i}\}), where operatornamemex\\operatorname{mex} of a set of numbers SS is the smallest non-negative integer that does not occur in the set SS . In other words, the cost of a partition is the number of segments plus the sum of MEX over all segments. Let's define the value of an array b1,b2,,bkb_1, b_2, \ldots, b_k as the maximum possible cost over all partitions of this array. You are given an array aa of size nn . Find the sum of values of all its subsegments.An array xx is a subsegment of an array yy if xx can be obtained from yy by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

输入格式

The input contains several test cases. The first line contains one integer tt ( 1t301 \leq t \leq 30 ) — the number of test cases.

The first line for each test case contains one integer nn ( 1n1001 \leq n \leq 100 ) — the length of the array.

The second line contains a sequence of integers a1,a2,,ana_1, a_2, \ldots, a_n ( 0ai1090 \leq a_i \leq 10^9 ) — the array elements.

It is guaranteed that the sum of the values nn over all test cases does not exceed 100100 .

输出格式

For each test case print a single integer — the answer to the problem.

输入输出样例

  • 输入#1

    4
    2
    1 2
    3
    2 0 1
    4
    2 0 5 1
    5
    0 1 1 0 1

    输出#1

    4
    14
    26
    48

说明/提示

In the second test case:

  • The best partition for the subsegment [2,0,1][2, 0, 1] : [2],[0,1][2], [0, 1] . The cost of this partition equals to 2+mex({2})+mex({0,1})=2+0+2=42 + \operatorname{mex}(\{2\}) + \operatorname{mex}(\{0, 1\}) = 2 + 0 + 2 = 4 .
  • The best partition for the subsegment [2,0][2, 0] : [2],[0][2], [0] . The cost of this partition equals to 2+mex({2})+mex({0})=2+0+1=32 + \operatorname{mex}(\{2\}) + \operatorname{mex}(\{0\}) = 2 + 0 + 1 = 3
  • The best partition for the subsegment [2][2] : [2][2] . The cost of this partition equals to 1+mex({2})=1+0=11 + \operatorname{mex}(\{2\}) = 1 + 0 = 1 .
  • The best partition for the subsegment [0,1][0, 1] : [0,1][0, 1] . The cost of this partition equals to 1+mex({0,1})=1+2=31 + \operatorname{mex}(\{0, 1\}) = 1 + 2 = 3 .
  • The best partition for the subsegment [0][0] : [0][0] . The cost of this partition equals to 1+mex({0})=1+1=21 + \operatorname{mex}(\{0\}) = 1 + 1 = 2 .
  • The best partition for the subsegment [1][1] : [1][1] . The cost of this partition equals to 1+mex({1})=1+0=11 + \operatorname{mex}(\{1\}) = 1 + 0 = 1 .

The sum of values over all subsegments equals to 4+3+1+3+2+1=144 + 3 + 1 + 3 + 2 + 1 = 14 .

首页