This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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[i] >= a[q[c].f]) {
Mi = min(Mi, q[c].s);
c --;
}
dp[_][i] = Mi + a[i];
dp[_][i] = min(dp[_][i], dp[_][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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |