D. Omkar and Circle

时间:2020-07-12
本文章向大家介绍 D. Omkar and Circle,主要包括 D. Omkar and Circle使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!

You are given \(n\) nonnegative integers \(a1,a2,…,an\) arranged in a circle, where nn must be odd (ie. \(n−1\) is divisible by \(2\)). Formally, for all \(i\) such that \(2≤i≤n\), the elements \(ai−1\) and \(ai\) are considered to be adjacent, and \(an\) and \(a1\) are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.

Help Danny find the maximum possible circular value after some sequences of operations.

Input

The first line contains one odd integer \(n\) (\(1≤n<2⋅10^5\), \(n\) is odd) — the initial size of the circle.

The second line contains nn integers \(a1,a2,…,an\) (\(0≤ai≤10^9\)) — the initial numbers in the circle.

Output

Output the maximum possible circular value after applying some sequence of operations to the given circle.

Examples

input

3
7 10 2

output

17

input

1
4

output

4

Note

For the first test case, here's how a circular value of 1717 is obtained:

Pick the number at index 33. The sum of adjacent elements equals 1717. Delete 77 and 1010 from the circle and replace 22 with 1717.

Note that the answer may not fit in a 3232-bit integer.

题解

第一感觉,这个题和合并果子的那道题很像,可以尝试使用优先队列来写,合并的时候,优先合并当前位置元素小的,结果 \(wa\) 了。

优化一下需要求解的东西,

\(n\) 个物品,需要合并 \(n/2\)

每次选择合并的位置不能是相邻的,

\(namo\) 就可以选择以求前缀和的方式来求解不相邻的 \(n/2\) 个的最小值,拿总之减一下,或者是求一下减之后最大值。

代码

#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 4e5+55;
ll a[maxn];
ll c[maxn];
ll d[maxn];
int main(){
    int n;
    ll sum=0;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        scanf("%lld",&a[i]);
        sum+=a[i];
    }
    for(int i=0;i<n;++i){
        if(i>=2)c[i]=c[i-2]+a[i];
        else c[i]=a[i];
    }
    for(int i=n-1;i>=0;--i){
        if(i<=n-3)d[i]=d[i+2]+a[i];
        else d[i]=a[i];
    }
    ll maxx=c[0];
    for(int i=0;i<n;++i){
        maxx=max(maxx,c[i]+d[i+1]);
    }
    printf("%lld\n",maxx);
    return 0;
}

原文地址:https://www.cnblogs.com/VagrantAC/p/13287236.html