#include <bits/stdc++.h>
#define F first
#define S second
#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define per(i, a, b) for(int i = (b)-1; i >= (a); --i)
#define bck(i, a, b) if ((i) >= (a) && (i) < (b))
#define trav(x, a) for (auto &x : (a))
#define sz(a) (int)(a).size()
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define dbg(X) std::cerr << "[DBG]("<<__FUNCTION__<<":"<<__LINE__<<") " << #X << ": " << X << std::endl;
using namespace std;
typedef long long ll;
typedef string str;
template<typename T> using vec = vector<T>;
template<typename T> using pq = priority_queue<T, vector<T>, std::greater<T>>;
template<typename T> using mxpq = priority_queue<T>;
typedef pair<int,int> pii;
typedef vec<int> vi;
typedef vec<pii> vii;
typedef vec<vi> vvi;
typedef pair<ll,ll> pll;
typedef vec<ll> vl;
typedef vec<pll> vll;
typedef vec<vl> vvl;
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<typename A, typename B>
istream& operator>>(istream& s, pair<A,B>& p) { return s>>p.first>>p.second; }
template<typename A, typename B>
ostream& operator<<(ostream& s, const pair<A,B>& p) { return s<<"{"<<p.first<<", "<<p.second<<"}"; }
template<typename T>
istream& operator>>(istream& s, vec<T>& p) { for (T& t : p) s >> t; return s; }
template<typename T>
ostream& operator<<(ostream& s, const vec<T>& p) { for (const T& t : p) s << t << " "; return s; }
pll go(vl& a, ll cost) {
int n = sz(a);
vl dp0(n),dp1(n), cnt1(n), cnt0(n);
dp0[0] = 0;
dp1[0] = a[0] - cost;
cnt0[0]=0;
cnt1[0]=1;
rep(i, 1, n) {
dp0[i] = max(dp0[i-1], dp1[i-1]);
if (dp0[i-1] >= dp1[i-1]) cnt0[i] = cnt0[i-1];
else cnt0[i] = cnt1[i-1];
dp1[i] = max(dp0[i-1] - cost, dp1[i-1]) + a[i];
if (dp0[i-1] - cost > dp1[i-1]) {
cnt1[i] = cnt0[i-1] + 1;
} else {
cnt1[i] = cnt1[i-1];
}
}
if (dp0.back() >= dp1.back()) {
return {dp0.back(), cnt0.back()};
} else {
return {dp1.back(), cnt1.back()};
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n, k; cin >> n >> k;
vl a(n); cin >> a;
/* cout << go(a, 0) << endl; */
/* cout << go(a, 1) << endl; */
/* cout << go(a, 2) << endl; */
/* cout << go(a, 3) << endl; */
ll lo = 0, hi = 1e12;
while (hi - lo > 1) {
ll m = (lo + hi)/2;
ll x = go(a, m).S;
if (x >= k) lo = m;
else hi=m;
}
cout << go(a, lo).F + lo*k << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
236 ms |
14596 KB |
Output is correct |
2 |
Correct |
265 ms |
14836 KB |
Output is correct |
3 |
Correct |
247 ms |
15112 KB |
Output is correct |
4 |
Correct |
243 ms |
14892 KB |
Output is correct |
5 |
Correct |
248 ms |
14868 KB |
Output is correct |
6 |
Correct |
258 ms |
14580 KB |
Output is correct |
7 |
Correct |
241 ms |
14688 KB |
Output is correct |
8 |
Correct |
243 ms |
14900 KB |
Output is correct |
9 |
Correct |
240 ms |
14808 KB |
Output is correct |
10 |
Correct |
262 ms |
14864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
264 ms |
12952 KB |
Output is correct |
2 |
Correct |
278 ms |
13264 KB |
Output is correct |
3 |
Correct |
241 ms |
13076 KB |
Output is correct |
4 |
Correct |
242 ms |
13124 KB |
Output is correct |
5 |
Correct |
258 ms |
14572 KB |
Output is correct |
6 |
Correct |
244 ms |
13092 KB |
Output is correct |
7 |
Correct |
255 ms |
13268 KB |
Output is correct |
8 |
Correct |
262 ms |
14920 KB |
Output is correct |
9 |
Correct |
284 ms |
14660 KB |
Output is correct |
10 |
Correct |
260 ms |
13168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
298 ms |
15024 KB |
Output is correct |
2 |
Correct |
279 ms |
14804 KB |
Output is correct |
3 |
Correct |
299 ms |
15056 KB |
Output is correct |
4 |
Correct |
298 ms |
14856 KB |
Output is correct |
5 |
Correct |
354 ms |
14996 KB |
Output is correct |
6 |
Correct |
301 ms |
15212 KB |
Output is correct |
7 |
Correct |
303 ms |
15104 KB |
Output is correct |
8 |
Correct |
293 ms |
14960 KB |
Output is correct |
9 |
Correct |
292 ms |
15168 KB |
Output is correct |
10 |
Correct |
297 ms |
15228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
356 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
236 ms |
14596 KB |
Output is correct |
2 |
Correct |
265 ms |
14836 KB |
Output is correct |
3 |
Correct |
247 ms |
15112 KB |
Output is correct |
4 |
Correct |
243 ms |
14892 KB |
Output is correct |
5 |
Correct |
248 ms |
14868 KB |
Output is correct |
6 |
Correct |
258 ms |
14580 KB |
Output is correct |
7 |
Correct |
241 ms |
14688 KB |
Output is correct |
8 |
Correct |
243 ms |
14900 KB |
Output is correct |
9 |
Correct |
240 ms |
14808 KB |
Output is correct |
10 |
Correct |
262 ms |
14864 KB |
Output is correct |
11 |
Correct |
264 ms |
12952 KB |
Output is correct |
12 |
Correct |
278 ms |
13264 KB |
Output is correct |
13 |
Correct |
241 ms |
13076 KB |
Output is correct |
14 |
Correct |
242 ms |
13124 KB |
Output is correct |
15 |
Correct |
258 ms |
14572 KB |
Output is correct |
16 |
Correct |
244 ms |
13092 KB |
Output is correct |
17 |
Correct |
255 ms |
13268 KB |
Output is correct |
18 |
Correct |
262 ms |
14920 KB |
Output is correct |
19 |
Correct |
284 ms |
14660 KB |
Output is correct |
20 |
Correct |
260 ms |
13168 KB |
Output is correct |
21 |
Correct |
298 ms |
15024 KB |
Output is correct |
22 |
Correct |
279 ms |
14804 KB |
Output is correct |
23 |
Correct |
299 ms |
15056 KB |
Output is correct |
24 |
Correct |
298 ms |
14856 KB |
Output is correct |
25 |
Correct |
354 ms |
14996 KB |
Output is correct |
26 |
Correct |
301 ms |
15212 KB |
Output is correct |
27 |
Correct |
303 ms |
15104 KB |
Output is correct |
28 |
Correct |
293 ms |
14960 KB |
Output is correct |
29 |
Correct |
292 ms |
15168 KB |
Output is correct |
30 |
Correct |
297 ms |
15228 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
312 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
312 KB |
Output is correct |
44 |
Correct |
1 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
316 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
1 ms |
204 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
1 ms |
332 KB |
Output is correct |
52 |
Correct |
1 ms |
332 KB |
Output is correct |
53 |
Correct |
1 ms |
332 KB |
Output is correct |
54 |
Correct |
2 ms |
332 KB |
Output is correct |
55 |
Correct |
2 ms |
332 KB |
Output is correct |
56 |
Correct |
2 ms |
356 KB |
Output is correct |
57 |
Correct |
2 ms |
332 KB |
Output is correct |
58 |
Correct |
2 ms |
332 KB |
Output is correct |
59 |
Correct |
2 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
320 ms |
14800 KB |
Output is correct |
62 |
Correct |
322 ms |
15164 KB |
Output is correct |
63 |
Correct |
294 ms |
14840 KB |
Output is correct |
64 |
Correct |
321 ms |
15048 KB |
Output is correct |
65 |
Correct |
324 ms |
15032 KB |
Output is correct |
66 |
Correct |
308 ms |
15064 KB |
Output is correct |
67 |
Correct |
310 ms |
14848 KB |
Output is correct |
68 |
Correct |
271 ms |
15148 KB |
Output is correct |
69 |
Correct |
273 ms |
14936 KB |
Output is correct |
70 |
Correct |
261 ms |
14724 KB |
Output is correct |