题解归档 - cf104077J
本文最后由方少年更新于2026 年 6 月 28 日,已超过0天没有更新。如果文章内容或图片资源失效,请留言反馈,将会及时处理,谢谢!
题解归档 - cf104077J
本文由 cf-code 本地题解库自动归档;公开内容以本地 AC/验证版本为准。
- 本地编号:
cf104077J - 本地来源:
problems/cf104077J/idea.md - 题目链接:https://codeforces.com/gym/104077/problem/J
- 原始标题:J - Strange Sum
思路
J - Strange Sum
At most two elements can be selected.
If three elements were selected, take the selected element with the largest index i. The interval[1,i] has length i and contains all selected elements up to that largest one, hence at least
three selected elements. This violates the condition for selecting a_i.
So the answer is just the sum of the largest two positive values, or fewer if positives are absent.
代码
来源:problems/cf104077J/solution.cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
ll best1 = 0, best2 = 0;
for (int i = 1; i <= n; i++) {
ll x;
cin >> x;
if (x > best1) {
best2 = best1;
best1 = x;
} else if (x > best2) {
best2 = x;
}
}
cout << best1 + best2 << '\n';
return 0;
}
~ ~ The End ~ ~
文章标题:题解归档 - cf104077J
文章链接:https://www.fangshaonian.cn/archives/155/
最后编辑:2026 年 6 月 28 日 19:02 By 方少年
许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)