(dp)Educational Codeforces Round 90 (Rated for Div. 2) D Maximum Sum on Even Positions

48 篇文章 0 订阅
7 篇文章 0 订阅

终于在cf写出了一道正经的dp题,值得庆贺( ̄▽ ̄)~*

dp[i]代表以i结尾的长度为偶数的贡献最大的反转区间,贡献是奇数位的和与偶数位的差

然后转移方程:奇数就是dp[i]=max(dp[i],dp[i-2]+a[i]-a[i-1]);偶数dp[i]=max(dp[i],dp[i-2]+a[i-1]-a[i]);
反转区间长度必为偶数,因为如果是奇数那么相当于没反转,那这么dp为什么能保证都选的是偶数长度的区间呢?

dp[0]不选,dp[1]=max(0,dp[1]-dp[0])即dp[1]只能选0个或两个,之后都是两个数两个数往前面dp[i-2]连或不连,所以必为偶

答案就是原序列偶数位和加上dp[i]中最大值

 You are given an array a consisting of n integers. Indices of the array start from zero (i. e. the first element is a0, the second one is a1

, and so on).

You can reverse at most one subarray (continuous subsegment) of this array. Recall that the subarray of a

with borders l and r is a[l;r]=al,al+1,…,ar

.

Your task is to reverse such a subarray that the sum of elements on even positions of the resulting array is maximized (i. e. the sum of elements a0,a2,…,a2k

for integer k=⌊n−12⌋

should be maximum possible).

You have to answer t

independent test cases.

Input

The first line of the input contains one integer t

(1≤t≤2⋅104) — the number of test cases. Then t

test cases follow.

The first line of the test case contains one integer n

(1≤n≤2⋅105) — the length of a. The second line of the test case contains n integers a0,a1,…,an−1 (1≤ai≤109), where ai is the i-th element of a

.

It is guaranteed that the sum of n

does not exceed 2⋅105 (∑n≤2⋅105

).

Output

For each test case, print the answer on the separate line — the maximum possible sum of elements on even positions after reversing at most one subarray (continuous subsegment) of a

.

Example

Input

Copy

4
8
1 7 3 4 7 6 2 9
5
1 2 1 2 1
10
7 8 4 5 7 6 8 9 7 3
4
3 1 2 1

Output

Copy

26
5
37
5
    #include <cstdio>
    #include<iostream>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    #include<set>
    #include<vector>
    using namespace std;
    const int maxn=1505000;
    #define mod 1000000007
    #include<stdio.h>
    #define ll long long
    ll dp[maxn],a[maxn];
    int main()
    {
        ll t;
        scanf("%lld",&t);
        while(t--)
        {
            ll n;
            scanf("%lld",&n);
            for(ll i=0;i<n;i++) scanf("%lld",&a[i]),dp[i]=0;
            if(n==1)
            {
                printf("%lld\n",a[0]);
                continue;
            }
            dp[0]=0;dp[1]=max(dp[1],a[1]-a[0]);
            ll mx=max(0ll,dp[1]);
            for(ll i=2;i<n;i++)
            {
                if(i%2==0)
                    dp[i]=max(dp[i],dp[i-2]+a[i-1]-a[i]);
                else
                    dp[i]=max(dp[i],dp[i-2]+a[i]-a[i-1]);
                //printf("dp[%lld]=%lld\n",i,dp[i]);
                mx=max(mx,dp[i]);
            }
            ll ans=0;
            for(ll i=0;i<n;i+=2) ans+=a[i];
            printf("%lld\n",ans+mx);
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值