#include <cstdlib>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define com complex<double>
#define ld long double;
#define ii pair<ll,ll>
#define vi vector<int>
#define vii vector<ii>
#define vc vector<char>
#define vs vector<string>
#define vd vector<double>
#define vcom vector<com>
#define vld vector<ld>
#define vll vector<ll>
#define vvi vector<vi>
#define vvii vector<vii>
#define vvc vector<vc>
#define vvs vector<vs>
#define vvll vector<vll>
#define vvd vector<vd>
#define vvcom vector<vcom>
#define vvld vector<vld>
#define FOR(x,n) for(int x=0;x<(n);x++)
#define FORS(x,n) for(int x=1;x<=(n);x++)
#define FORE(x,a) for(auto &x: a)
#define FORT(x,a,b) for(int x=(a);x<(b);x++)
#define FORD(x,a,b) for(int x=(a);x>=(b);x--)
#define ALL(x) x.begin(),x.end()
#define REP(n) for(int _ = 0; _ < n; _++)
#define MT make_tuple
#define MP make_pair
#define pb push_back
#define endl '\n'
#define F first
#define S second
#define vvvll vector<vvll>
#define sz(x) ((int)x.size())
pair<ll,ll> relax(ll p, vll &a) {
int n = a.size();
ll cv = 0; // curval
ll cb = 0; // curbest
ll t = 0, cnt = 0;
FOR(i,n) {
cv += a[i];
if(cv < cb-p && cb > p) {
cnt += 1;
t += cb-p;
cv = 0;
cb = 0;
} else {
cb = max(cb,cv);
if(cv < 0) {
cv = 0;
}
}
}
if(cb > p) {
cnt += 1;
t += cb-p;
cv = 0;
}
return {t,cnt};
}
void solve() {
int n,k; cin >> n >> k;
vll a(n);
FOR(i,n) cin >> a[i];
int cp = 0;
ll pv = 0;
FOR(i,n) {
if(a[i] > 0) {
cp++;
pv += a[i];
}
}
if(cp <= k) {
cout << pv << endl;
return;
}
ll mi = 0, ma = 1e12;
while(mi < ma ){
ll t,cnt;
ll mid = (mi+ma)/2;
tie(t,cnt) = relax(mid,a);
//cout << mid << " " << t << " " << cnt << endl;
if(cnt <= k) {
ma = mid;
} else {
mi = mid + 1;
}
}
ll t,cnt;
tie(t,cnt) = relax(mi,a);
cout << t + cnt*mi << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
//cin >> t;
while(t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
2516 KB |
Output is correct |
2 |
Correct |
57 ms |
2644 KB |
Output is correct |
3 |
Correct |
51 ms |
2644 KB |
Output is correct |
4 |
Correct |
45 ms |
2644 KB |
Output is correct |
5 |
Correct |
44 ms |
2644 KB |
Output is correct |
6 |
Correct |
44 ms |
2516 KB |
Output is correct |
7 |
Correct |
44 ms |
2516 KB |
Output is correct |
8 |
Correct |
44 ms |
2644 KB |
Output is correct |
9 |
Correct |
52 ms |
2516 KB |
Output is correct |
10 |
Correct |
42 ms |
2516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
2516 KB |
Output is correct |
2 |
Correct |
38 ms |
2644 KB |
Output is correct |
3 |
Correct |
32 ms |
2516 KB |
Output is correct |
4 |
Correct |
36 ms |
2516 KB |
Output is correct |
5 |
Correct |
46 ms |
2516 KB |
Output is correct |
6 |
Correct |
36 ms |
2516 KB |
Output is correct |
7 |
Correct |
37 ms |
2644 KB |
Output is correct |
8 |
Correct |
43 ms |
2644 KB |
Output is correct |
9 |
Correct |
43 ms |
2516 KB |
Output is correct |
10 |
Correct |
42 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
2644 KB |
Output is correct |
2 |
Correct |
49 ms |
5540 KB |
Output is correct |
3 |
Correct |
52 ms |
5580 KB |
Output is correct |
4 |
Correct |
56 ms |
5532 KB |
Output is correct |
5 |
Correct |
48 ms |
5572 KB |
Output is correct |
6 |
Correct |
59 ms |
5580 KB |
Output is correct |
7 |
Correct |
49 ms |
5596 KB |
Output is correct |
8 |
Correct |
48 ms |
5580 KB |
Output is correct |
9 |
Correct |
51 ms |
5600 KB |
Output is correct |
10 |
Correct |
49 ms |
5592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
356 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
2516 KB |
Output is correct |
2 |
Correct |
57 ms |
2644 KB |
Output is correct |
3 |
Correct |
51 ms |
2644 KB |
Output is correct |
4 |
Correct |
45 ms |
2644 KB |
Output is correct |
5 |
Correct |
44 ms |
2644 KB |
Output is correct |
6 |
Correct |
44 ms |
2516 KB |
Output is correct |
7 |
Correct |
44 ms |
2516 KB |
Output is correct |
8 |
Correct |
44 ms |
2644 KB |
Output is correct |
9 |
Correct |
52 ms |
2516 KB |
Output is correct |
10 |
Correct |
42 ms |
2516 KB |
Output is correct |
11 |
Correct |
39 ms |
2516 KB |
Output is correct |
12 |
Correct |
38 ms |
2644 KB |
Output is correct |
13 |
Correct |
32 ms |
2516 KB |
Output is correct |
14 |
Correct |
36 ms |
2516 KB |
Output is correct |
15 |
Correct |
46 ms |
2516 KB |
Output is correct |
16 |
Correct |
36 ms |
2516 KB |
Output is correct |
17 |
Correct |
37 ms |
2644 KB |
Output is correct |
18 |
Correct |
43 ms |
2644 KB |
Output is correct |
19 |
Correct |
43 ms |
2516 KB |
Output is correct |
20 |
Correct |
42 ms |
2644 KB |
Output is correct |
21 |
Correct |
48 ms |
2644 KB |
Output is correct |
22 |
Correct |
49 ms |
5540 KB |
Output is correct |
23 |
Correct |
52 ms |
5580 KB |
Output is correct |
24 |
Correct |
56 ms |
5532 KB |
Output is correct |
25 |
Correct |
48 ms |
5572 KB |
Output is correct |
26 |
Correct |
59 ms |
5580 KB |
Output is correct |
27 |
Correct |
49 ms |
5596 KB |
Output is correct |
28 |
Correct |
48 ms |
5580 KB |
Output is correct |
29 |
Correct |
51 ms |
5600 KB |
Output is correct |
30 |
Correct |
49 ms |
5592 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
316 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
320 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
320 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
324 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
356 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
82 ms |
5540 KB |
Output is correct |
62 |
Correct |
89 ms |
5604 KB |
Output is correct |
63 |
Correct |
52 ms |
5524 KB |
Output is correct |
64 |
Correct |
76 ms |
5588 KB |
Output is correct |
65 |
Correct |
68 ms |
5640 KB |
Output is correct |
66 |
Correct |
64 ms |
5576 KB |
Output is correct |
67 |
Correct |
69 ms |
5568 KB |
Output is correct |
68 |
Correct |
50 ms |
5580 KB |
Output is correct |
69 |
Correct |
48 ms |
5528 KB |
Output is correct |
70 |
Correct |
47 ms |
5528 KB |
Output is correct |