#include <bits/stdc++.h>
#define int ll
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<ll, ll>
#define MX(a,b) a=max(a,b)
#define MN(a,b) a=min(a,b)
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define REP(i,n) for (int i = 0; i<n; ++i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define pb push_back
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
const int maxn = 3e5+5;
const ll inf = 1ll<<61;
ll A[maxn];
pii dp[maxn][2];
int n;
pair<ll, ll> operator + (pair<ll, ll> a, pair<ll, ll> b) {
return {a.f + b.f, a.s + b.s};
}
pair<ll, ll> get(int p) {
// beware of initial states!!
dp[0][0] = {0, 0}; // no value, nothing taken yet
dp[0][1] = {-inf, -inf};
REP1(i,n) {
dp[i][0] = dp[i][1] = {-inf, -inf};
MX(dp[i][0], dp[i-1][0]); // continue not taking anything
MX(dp[i][0], dp[i-1][0] + make_pair(-p, -1)); // take an empty segment (?)
MX(dp[i][0], dp[i-1][1]); // stop taking stuff
MX(dp[i][1], dp[i-1][0] + make_pair(-p + A[i], -1));
MX(dp[i][1], dp[i-1][1] + make_pair(-p + A[i], -1));
MX(dp[i][1], dp[i-1][1] + make_pair(A[i], 0));
}
return max(dp[n][0], dp[n][1]);
}
inline ll DIV(ll a, ll b) {
return a/b - ((a%b) && ((a^b)<0));
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
bug(1,2);
int k; cin>>n>>k;
REP1(i,n) {
cin>>A[i];
}
ll l = 0, r = 3e14+10000;
while (l!=r) {
ll md = DIV((l+r),2);
pair<ll, ll> gt = get(md);
if (-gt.s > k) {
// taking too many zones, gotta make the penalty higher
l = md+1;
}else{
r = md;
}
}
pair<ll, ll> gt = get(l);
// only linear difference from it
bug(l, gt.f, -gt.s);
ll re = gt.f + (k) * (l);
cout<<re<<endl;
}
/*
3 1
5 -9 5
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
11596 KB |
Output is correct |
2 |
Correct |
141 ms |
11896 KB |
Output is correct |
3 |
Correct |
154 ms |
11980 KB |
Output is correct |
4 |
Correct |
164 ms |
11952 KB |
Output is correct |
5 |
Correct |
141 ms |
11984 KB |
Output is correct |
6 |
Correct |
153 ms |
11688 KB |
Output is correct |
7 |
Correct |
144 ms |
11684 KB |
Output is correct |
8 |
Correct |
137 ms |
11824 KB |
Output is correct |
9 |
Correct |
142 ms |
11644 KB |
Output is correct |
10 |
Correct |
153 ms |
11832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
11800 KB |
Output is correct |
2 |
Correct |
164 ms |
12020 KB |
Output is correct |
3 |
Correct |
142 ms |
11744 KB |
Output is correct |
4 |
Correct |
140 ms |
11848 KB |
Output is correct |
5 |
Correct |
147 ms |
11804 KB |
Output is correct |
6 |
Correct |
148 ms |
11732 KB |
Output is correct |
7 |
Correct |
138 ms |
12108 KB |
Output is correct |
8 |
Correct |
143 ms |
11968 KB |
Output is correct |
9 |
Correct |
148 ms |
11668 KB |
Output is correct |
10 |
Correct |
139 ms |
11860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
11916 KB |
Output is correct |
2 |
Correct |
189 ms |
14744 KB |
Output is correct |
3 |
Correct |
182 ms |
14884 KB |
Output is correct |
4 |
Correct |
167 ms |
14756 KB |
Output is correct |
5 |
Correct |
185 ms |
14888 KB |
Output is correct |
6 |
Correct |
227 ms |
15100 KB |
Output is correct |
7 |
Correct |
176 ms |
15036 KB |
Output is correct |
8 |
Correct |
193 ms |
14856 KB |
Output is correct |
9 |
Correct |
178 ms |
15052 KB |
Output is correct |
10 |
Correct |
176 ms |
15036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
344 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
468 KB |
Output is correct |
26 |
Correct |
3 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
11596 KB |
Output is correct |
2 |
Correct |
141 ms |
11896 KB |
Output is correct |
3 |
Correct |
154 ms |
11980 KB |
Output is correct |
4 |
Correct |
164 ms |
11952 KB |
Output is correct |
5 |
Correct |
141 ms |
11984 KB |
Output is correct |
6 |
Correct |
153 ms |
11688 KB |
Output is correct |
7 |
Correct |
144 ms |
11684 KB |
Output is correct |
8 |
Correct |
137 ms |
11824 KB |
Output is correct |
9 |
Correct |
142 ms |
11644 KB |
Output is correct |
10 |
Correct |
153 ms |
11832 KB |
Output is correct |
11 |
Correct |
151 ms |
11800 KB |
Output is correct |
12 |
Correct |
164 ms |
12020 KB |
Output is correct |
13 |
Correct |
142 ms |
11744 KB |
Output is correct |
14 |
Correct |
140 ms |
11848 KB |
Output is correct |
15 |
Correct |
147 ms |
11804 KB |
Output is correct |
16 |
Correct |
148 ms |
11732 KB |
Output is correct |
17 |
Correct |
138 ms |
12108 KB |
Output is correct |
18 |
Correct |
143 ms |
11968 KB |
Output is correct |
19 |
Correct |
148 ms |
11668 KB |
Output is correct |
20 |
Correct |
139 ms |
11860 KB |
Output is correct |
21 |
Correct |
179 ms |
11916 KB |
Output is correct |
22 |
Correct |
189 ms |
14744 KB |
Output is correct |
23 |
Correct |
182 ms |
14884 KB |
Output is correct |
24 |
Correct |
167 ms |
14756 KB |
Output is correct |
25 |
Correct |
185 ms |
14888 KB |
Output is correct |
26 |
Correct |
227 ms |
15100 KB |
Output is correct |
27 |
Correct |
176 ms |
15036 KB |
Output is correct |
28 |
Correct |
193 ms |
14856 KB |
Output is correct |
29 |
Correct |
178 ms |
15052 KB |
Output is correct |
30 |
Correct |
176 ms |
15036 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
328 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
328 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
2 ms |
340 KB |
Output is correct |
52 |
Correct |
2 ms |
336 KB |
Output is correct |
53 |
Correct |
2 ms |
344 KB |
Output is correct |
54 |
Correct |
2 ms |
340 KB |
Output is correct |
55 |
Correct |
3 ms |
468 KB |
Output is correct |
56 |
Correct |
3 ms |
340 KB |
Output is correct |
57 |
Correct |
3 ms |
340 KB |
Output is correct |
58 |
Correct |
2 ms |
340 KB |
Output is correct |
59 |
Correct |
2 ms |
340 KB |
Output is correct |
60 |
Correct |
2 ms |
336 KB |
Output is correct |
61 |
Correct |
220 ms |
14732 KB |
Output is correct |
62 |
Correct |
253 ms |
15096 KB |
Output is correct |
63 |
Correct |
182 ms |
14692 KB |
Output is correct |
64 |
Correct |
203 ms |
14980 KB |
Output is correct |
65 |
Correct |
204 ms |
15012 KB |
Output is correct |
66 |
Correct |
225 ms |
14856 KB |
Output is correct |
67 |
Correct |
202 ms |
14708 KB |
Output is correct |
68 |
Correct |
199 ms |
14976 KB |
Output is correct |
69 |
Correct |
185 ms |
14708 KB |
Output is correct |
70 |
Correct |
173 ms |
14636 KB |
Output is correct |