This page is still under construction.

Parts of this page are still being built. What you see may change.

Sum of LIS lengths over every consecutive subsequence

Time limit1sMemory limit128 MB

Summary
Sum the LIS lengths of all contiguous subarrays for each test case of distinct integers.
Level

Medium6 of 10

Topics
Dynamic programming, Binary search, Brute force
Solved
No attempts yet

Problem

Mr. C is interested in the longest increasing subsequence problem. A sequence S=s1,s2,…,sNS = s_1, s_2, \ldots, s_N is given. A subsequence L=l1,l2,…,lkL = l_1, l_2, \ldots, l_k of SS is increasing when l1<l2<⋯<lkl_1 < l_2 < \cdots < l_k, and the longest increasing subsequence of SS is its LIS.

A consecutive subsequence of SS is a subsequence whose elements are adjacent in the original sequence. Find the LIS length of every consecutive subsequence of non-zero length, then add all of those lengths together.

Input

The first line contains an integer TT, the number of cases. It is followed by TT blocks, each one case.

The first line of each case contains an integer NN (1≤N≤5001 \le N \le 500), the length of SS.

The next NN lines each contain an integer sis_i (1≤si≤N1 \le s_i \le N), the ii-th element of SS. Every element of SS is distinct.

Output

Print TT lines, one per case, in the same order as the input.

The ii-th line has the format Case #i: X, where ii is the case number and XX is the total LIS length over every consecutive subsequence of non-zero length in that case.

Examples1

  1. Example 1

    Input
    1
    3
    3
    1
    2
    
    Expected output
    Case #1: 8