Sherlock and Cost

Array A contains the elements, A1,A2…AN. And array B contains the elements, B1,B2…BN. There is a relationship between Ai and Bi, ∀ 1 ≤ i ≤ N, i.e.,
any element Ai lies between 1 and Bi.

Let the cost S of an array A be defined as:

You have to print the largest possible value of S.

Input Format

The first line contains, T, the number of test cases. Each test case contains an integer, N, in first line. The second line of each test case contains N integers that denote the array B.

Constraints

1 ≤ T ≤ 20
1 ≤ N ≤ 105
1 ≤ Bi ≤ 100

Output Format

For each test case, print the required answer in one line.

Sample Input

1
5
10 1 10 1 10
Sample Output

36
Explanation

The maximum value occurs when A1=A3=A5=10 and A2=A4=1.

#include
using namespace std;
int ar[100005]={},dp[100005][2]={};
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n,i,j;
        cin >> n;
        for(i=0; i
            cin >> ar[i];
        for(i=0; i
        {
            dp[i+1][0]=max(dp[i][0],dp[i][1]+abs(ar[i]-1));
            dp[i+1][1]=max(dp[i][0]+abs(ar[i+1]-1),dp[i][1]+abs(ar[i]-ar[i+1]));
        }
        cout << max(dp[n-1][0],dp[n-1][1]) << endl;
    }
    return 0;
}

你可能感兴趣的:(acm)