#P16895. [GKS 2022 #G] Happy Subarrays

[GKS 2022 #G] Happy Subarrays

Problem Description

Let us define F(B,L,R)F(B, L, R) as the sum of a subarray of an array BB bounded by indices LL and RR (both inclusive). Formally, F(B,L,R)=BL+BL+1++BRF(B, L, R) = B_L + B_{L+1} + \cdots + B_R.

An array CC of length KK is called a happy array if all the prefix sums of CC are non-negative. Formally, the terms F(C,1,1),F(C,1,2),,F(C,1,K)F(C, 1, 1), F(C, 1, 2), \ldots, F(C, 1, K) are all non-negative.

Given an array AA of NN integers, find the result of adding the sums of all the happy subarrays in the array AA.

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow.

Each test case begins with one line consisting an integer NN denoting the number of integers in the input array AA. Then the next line contains NN integers A1,A2,,ANA_1, A_2, \ldots, A_N representing the integers in given input array AA.

Output Format

For each test case, output one line containing Case #xx: yy, where xx is the test case number (starting from 11) and yy is the result of adding the sums of all happy subarrays in the given input array AA.

2
5
1 -2 3 -2 4
3
1 0 3
Case #1: 14
Case #2: 12

Hint

In Sample Case # 11, the happy subarrays are [11], [33], [3,23, -2], [3,2,43, -2, 4], and [44] with their respective sums 11, 33, 11, 55, and 44. After adding the sums obtained, the result is 1414.

In Sample Case # 22, the happy subarrays are [11], [1,01, 0], [1,0,31, 0, 3], [00], [0,30, 3], and [33] with their respective sums 11, 11, 44, 00, 33, and 33. After adding the sums obtained, the result is 1212.

Limits

1T1001 \le T \le 100.

800Ai800-800 \le A_i \le 800, for all ii.

Test Set 1

1N2001 \le N \le 200.

Test Set 2

For at most 3030 cases:

1N4×1051 \le N \le 4 \times 10^5.

For the remaining cases:

1N2001 \le N \le 200.