#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
int main() {
// alien trick
int n, k;
cin >> n >> k;
int a[n + 1];
for(int i = 1; i <= n; ++i)
cin >> a[i];
a[0] = a[1];
// consider cost as dp[i]
// maintain the minimum amount to achieve the desired minimum cost
// dp[i] = min(dp[j] + cost(j, i) - penalty)
int lb = 0, rb = 1e9, ans = -1;
while(lb <= rb) {
int mb = (lb + rb) / 2;
// maintain rolling aja harusnya bs, tapi agk tricky karena ada shiftnya
// actually tinggal maintain posisi T aja as negative
pair<ll, int> curm = {-a[1], 0};
pair<ll, int> dp[n + 1];
//cout << mb << endl;
for(int i = 1; i <= n; ++i) {
dp[i] = {curm.fi + a[i] + mb + 1, curm.se + 1};
//cout << "OUTPUT" << endl;
//cout << curm.fi << " "<< a[i] << " " << mb << endl;
//cout << curm.fi << " " << curm.se << " " << dp[i].fi - a[i] << " " << dp[i].se << endl;
curm = min(curm, {dp[i].fi - a[i + 1], dp[i].se});
}
if(dp[n].se <= k) {
// tambah penalti -> less k
ans = mb;
rb = mb - 1;
}
else
lb = mb + 1;
}
pair<ll, int> curm = {-a[1], 0};
pair<ll, int> dp[n + 1];
//cout << ans << endl;
for(int i = 1; i <= n; ++i) {
dp[i] = {curm.fi + a[i] + ans + 1, curm.se + 1};
//cout << i << endl;
//cout << i << " " << dp[i].fi << " " << dp[i].se << endl;
//cout << curm.fi << " " << curm.se << " " << dp[i].fi - a[i] << " " << dp[i].se << endl;
curm = min(curm, {dp[i].fi - a[i + 1], dp[i].se});
}
ll res = dp[n].fi;
//cout << res << " ";
res -= 1ll * k * ans;
cout << res << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
296 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 |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
296 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 |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
312 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
296 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 |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
312 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
45 ms |
3088 KB |
Output is correct |
17 |
Correct |
53 ms |
3196 KB |
Output is correct |
18 |
Correct |
50 ms |
3092 KB |
Output is correct |
19 |
Correct |
53 ms |
3096 KB |
Output is correct |
20 |
Correct |
52 ms |
3096 KB |
Output is correct |
21 |
Correct |
47 ms |
3084 KB |
Output is correct |
22 |
Correct |
49 ms |
3160 KB |
Output is correct |
23 |
Correct |
52 ms |
3080 KB |
Output is correct |
24 |
Correct |
48 ms |
3088 KB |
Output is correct |