/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC target ("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#include<bits/stdc++.h>
#define ld long double
#define sz(v) (int)v.size()
#define ll long long
#define pb push_back
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define nl "\n"
using namespace std;
using pii = pair<int, int>;
const int N = (int)1e6 + 7; // make sure this is right
const int M = (int)1e6 + 7;
const int inf = (int)4e9 + 7;
const ll INF = (ll)3e18 + 7;
const ll MOD = (ll)998244353; // make sure this is right
bool bit(int x, int i) {
return x >> i & 1;
}
int sum(int x, int y) {
x += y;
if(x >= MOD) x -= MOD;
return x;
}
pii dir[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int n, k, a[N];
int lo[N], mx[N][20];
ll dp[N][2];
int get(int l, int r) {
int x = lo[r - l + 1];
return max(mx[l][x], mx[r - (1 << x) + 1][x]);
}
void calc(int l, int r, int optl, int optr) {
if(l > r) return;
int mid = (l + r) >> 1, optk = -1;
for(int i = optl; i <= min(mid, optr); ++i) {
ll cur = dp[i - 1][0] + get(i, mid);
if(cur < dp[mid][1]) {
dp[mid][1] = cur;
optk = i;
}
}
calc(l, mid - 1, optl, optk);
calc(mid + 1, r, optk, optr);
}
void solve() {
cin >> n >> k;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
mx[i][0] = a[i];
}
for(int i = 1; i < 20; ++i) {
for(int j = 1; j <= n - (1 << i) + 1; ++j) {
mx[j][i] = max(mx[j][i - 1], mx[j + (1 << (i - 1))][i - 1]);
}
}
for(int i = 1; i <= n; ++i) dp[i][1] = dp[i][0] = INF;
for(int i = 1; i <= k; ++i) {
calc(1, n, 1, n);
for(int j = 1; j <= n; ++j) {
dp[j][0] = dp[j][1];
dp[j][1] = INF;
}
dp[0][0] = INF;
}
cout << dp[n][0];
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
for(int i = 2; i <= 1e6; ++i) lo[i] = lo[i / 2] + 1;
int test = 1;
//cin >> test;
for(int i = 1; i <= test; ++i) {
//cout << "Case #" << i << ": ";
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4180 KB |
Output is correct |
2 |
Correct |
3 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4248 KB |
Output is correct |
4 |
Incorrect |
3 ms |
4180 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4248 KB |
Output is correct |
2 |
Correct |
3 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Incorrect |
3 ms |
4180 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4180 KB |
Output is correct |
2 |
Correct |
3 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4248 KB |
Output is correct |
4 |
Incorrect |
3 ms |
4180 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4180 KB |
Output is correct |
2 |
Correct |
3 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4248 KB |
Output is correct |
4 |
Incorrect |
3 ms |
4180 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |