#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const int inf = 1e9 + 7;
const long long INF = (long long)1e18 + 7;
const int mod = 1e9 + 7;
void add(int &x, int y) {x += y; if (x >= mod) x -= mod;}
void sub(int &x, int y) {x -= y; if (x < 0) x += mod;}
int mul(int x, int y) {return 1LL * x * y % mod;}
// -----------------------------------------//
const int LOG = 17;
int n, k;
int f[N][LOG + 2], a[N];
void read() {
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
}
void build() {
for(int i = 1; i <= n; i++) f[i][0] = a[i];
for(int j = 1; j <= LOG; j++) {
for(int i = 1; i + MASK(j) - 1 <= n; i++) f[i][j] = max(f[i][j - 1], f[i + MASK(j - 1)][j - 1]);
}
}
int getMax(int l, int r) {
int k = log2(r - l + 1);
return max(f[l][k], f [r - MASK(k) + 1][k]);
}
void sub3() {
vector<vector<int>> dp;
dp.assign(n + 2, vector<int>(k + 2, inf));
dp[0][0] = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= min(k, i); j++) {
int mx = a[i];
for(int p = i; p >= 1; p--) {
mx = max(mx, a[p]);
minimize(dp[i][j], dp[p - 1][j - 1] + mx);
}
}
}
cout << dp[n][k];
}
void solve() {
// if (n <= 100) {
// sub3();
// return;
// }
vector<vector<int>> dp;
dp.assign(n + 2, vector<int>(k + 2, inf));
dp[0][0] = 0;
a[0] = inf;
for(int j = 1; j <= k; j++) {
stack<int> s1, s2;
s1.push(inf); s2.push(inf);
for(int i = 1; i <= n; i++) {
int val = dp[i - 1][j - 1];
while(a[i] >= s1.top()) {
val = min(val, s2.top());
s1.pop();
s2.pop();
}
if (s1.top() + s2.top() > val + a[i]) {
s1.push(a[i]);
s2.push(val);
}
dp[i][j] = s1.top() + s2.top();
}
}
cout << dp[n][k];
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
Compilation message (stderr)
blocks.cpp: In function 'int main()':
blocks.cpp:89:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:90:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |