#include <bits/stdc++.h>
#define file "main"
#define maxn (int) 1e5 + 5
#define pii pair<int, int>
#define ll long long
#define gb(i, j) ((i >> j) & 1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define f first
#define s second
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
if(fopen(file".inp", "r")) {
freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
}
int n, k;
int a[maxn];
int dp[105][maxn];
pii q[maxn];
void solve() {
cin >> n >> k;
for(int i = 1; i <= n; i ++) cin >> a[i];
for(int i = 0; i <= k; i ++) for(int j = 0; j <= n; j ++)
dp[i][j] = 1e9;
dp[0][0] = 0;
for(int _ = 1; _ <= k; _ ++) {
int d = 1, c = 0;
for(int i = 1; i <= n; i ++) {
int Mi = dp[_ - 1][i - 1];
while(d <= c && a[q[c].f] < a[i]) {
Mi = min(Mi, q[c].s);
c --;
}
dp[_][i] = Mi + a[i];
if(c) dp[_][i] = min(dp[_][i], dp[_ - 1][q[c].f]);
q[++ c] = {i, Mi};
}
}
cout << dp[k][n];
}
int main()
{
iof();
int nTest = 1;
// cin >> nTest;
while(nTest --) {
solve();
}
return 0;
}
Compilation message
blocks.cpp: In function 'void iof()':
blocks.cpp:22:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | freopen(file".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |