C. Particles
题意:给一个长度为n的序列,我们可以对序列进行的操作是删除中间的数并将两边的数相加,对于两端的数可以直接删除,最后结果只剩一个数,我们希望这个数最大。
思路:
需要知道只有下标奇偶性相同的才能融合, 其次可以跳着融合,所以我们需要份奇偶,遇到正数就选上,遇到负数就跳过,需要特判全是负数的情况,这种情况需要找到最小的负数。
#include <bits/stdc++.h>
#define LL long long
#define x first
#define y secondusing namespace std;
const int N = 2e5 + 10;
LL a[N], n, t;void solve()
{cin >> n;for(int i = 1; i <= n; ++ i) cin >> a[i];bool st = false;LL ans = 0, odd = 0, even = 0, maxx = -1e9;for(int i = 1; i <= n; ++ i){if(a[i] > 0){st = true;if(i & 1) odd += a[i];else even += a[i];}else maxx = max(maxx, a[i]);}if(st) cout << max(odd, even) << endl;else cout << maxx << endl;
}int main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// freopen("1.in", "r", stdin);cin >> t;while(t --) solve();return 0;
}