Codeforces Round #655 (Div. 2) D. Omkar and Circle

题目链接:https://codeforces.com/contest/1372/problem/D

题意

给出奇数个数围成的环,每次可以将一个数替换为相邻两个数的和并删除相邻的两个数,问最后余下的数的最大值。

题解

即从 $n$ 个数中选取 $frac{n+1}{2}$ 个数,且这些数中最多有一对数相邻的和的最大值。

代码

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
    int n; cin >> n;
    int a[n] = {};
    ll sum = 0, now = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        sum += a[i];
        if (i % 2 == 0) now += a[i];
    }
    ll ans = now;
    for (int i = 1; i < n; i++) {
        now = sum - now + a[i - 1];
        ans = max(ans, now);
    }
    cout << ans << "
";
}
原文地址:https://www.cnblogs.com/Kanoon/p/13289939.html