#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;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
2568 KB |
Output is correct |
2 |
Correct |
80 ms |
2612 KB |
Output is correct |
3 |
Correct |
81 ms |
2644 KB |
Output is correct |
4 |
Correct |
83 ms |
2516 KB |
Output is correct |
5 |
Correct |
83 ms |
2516 KB |
Output is correct |
6 |
Correct |
80 ms |
2568 KB |
Output is correct |
7 |
Correct |
81 ms |
2564 KB |
Output is correct |
8 |
Correct |
97 ms |
2608 KB |
Output is correct |
9 |
Correct |
86 ms |
2572 KB |
Output is correct |
10 |
Correct |
91 ms |
2516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
2516 KB |
Output is correct |
2 |
Correct |
45 ms |
2644 KB |
Output is correct |
3 |
Correct |
45 ms |
2516 KB |
Output is correct |
4 |
Correct |
43 ms |
2604 KB |
Output is correct |
5 |
Correct |
82 ms |
5324 KB |
Output is correct |
6 |
Correct |
42 ms |
3660 KB |
Output is correct |
7 |
Correct |
44 ms |
3660 KB |
Output is correct |
8 |
Correct |
81 ms |
5416 KB |
Output is correct |
9 |
Correct |
80 ms |
5232 KB |
Output is correct |
10 |
Correct |
44 ms |
3660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
2516 KB |
Output is correct |
2 |
Correct |
87 ms |
2576 KB |
Output is correct |
3 |
Correct |
89 ms |
2516 KB |
Output is correct |
4 |
Correct |
85 ms |
2516 KB |
Output is correct |
5 |
Correct |
86 ms |
2616 KB |
Output is correct |
6 |
Correct |
87 ms |
2616 KB |
Output is correct |
7 |
Correct |
91 ms |
2644 KB |
Output is correct |
8 |
Correct |
85 ms |
2516 KB |
Output is correct |
9 |
Correct |
88 ms |
2644 KB |
Output is correct |
10 |
Correct |
90 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
2568 KB |
Output is correct |
2 |
Correct |
80 ms |
2612 KB |
Output is correct |
3 |
Correct |
81 ms |
2644 KB |
Output is correct |
4 |
Correct |
83 ms |
2516 KB |
Output is correct |
5 |
Correct |
83 ms |
2516 KB |
Output is correct |
6 |
Correct |
80 ms |
2568 KB |
Output is correct |
7 |
Correct |
81 ms |
2564 KB |
Output is correct |
8 |
Correct |
97 ms |
2608 KB |
Output is correct |
9 |
Correct |
86 ms |
2572 KB |
Output is correct |
10 |
Correct |
91 ms |
2516 KB |
Output is correct |
11 |
Correct |
43 ms |
2516 KB |
Output is correct |
12 |
Correct |
45 ms |
2644 KB |
Output is correct |
13 |
Correct |
45 ms |
2516 KB |
Output is correct |
14 |
Correct |
43 ms |
2604 KB |
Output is correct |
15 |
Correct |
82 ms |
5324 KB |
Output is correct |
16 |
Correct |
42 ms |
3660 KB |
Output is correct |
17 |
Correct |
44 ms |
3660 KB |
Output is correct |
18 |
Correct |
81 ms |
5416 KB |
Output is correct |
19 |
Correct |
80 ms |
5232 KB |
Output is correct |
20 |
Correct |
44 ms |
3660 KB |
Output is correct |
21 |
Correct |
95 ms |
2516 KB |
Output is correct |
22 |
Correct |
87 ms |
2576 KB |
Output is correct |
23 |
Correct |
89 ms |
2516 KB |
Output is correct |
24 |
Correct |
85 ms |
2516 KB |
Output is correct |
25 |
Correct |
86 ms |
2616 KB |
Output is correct |
26 |
Correct |
87 ms |
2616 KB |
Output is correct |
27 |
Correct |
91 ms |
2644 KB |
Output is correct |
28 |
Correct |
85 ms |
2516 KB |
Output is correct |
29 |
Correct |
88 ms |
2644 KB |
Output is correct |
30 |
Correct |
90 ms |
2644 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |