이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
int main() {
int n, k;
cin >> n >> k;
vector<ll> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
// subtask 1 a_i > 0
if (*min_element(a.begin(), a.end()) >= 0) {
cout << accumulate(a.begin(), a.end(), 0LL) << endl;
return 0;
}
// subtask 2 at most one a_i < 0
if (count_if(a.begin(), a.end(), [](ll x) { return x < 0; }) <= 1) {
int pos =
find_if(a.begin(), a.end(), [](ll x) { return x < 0; }) - a.begin();
ll mx = 0;
mx = max(mx, accumulate(a.begin(), a.begin() + pos, 0LL));
mx = max(mx, accumulate(a.begin() + pos + 1, a.end(), 0LL));
mx = max(mx, accumulate(a.begin(), a.end(), 0LL));
if (k >= 2) mx = max(mx, accumulate(a.begin(), a.end(), 0LL) - a[pos]);
cout << mx << endl;
return 0;
}
// subtask 3 K = 1, maximum subarray
if (k == 1) {
ll mx = 0, cur = 0;
for (int i = 0; i < n; i++) {
cur += a[i];
mx = max(mx, cur);
cur = max(cur, 0LL);
}
cout << mx << endl;
return 0;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |