#include <bits/stdc++.h>
#define task "BriantheCrab"
#define int long long
#define pii pair <int, int>
#define fi first
#define se second
#define szf sizeof
#define sz(s) (int)((s).size())
using namespace std;
template <class T> void mini (T &t, T f) {if (t > f) t = f;}
template <class T> void maxi (T &t, T f) {if (t < f) t = f;}
const int maxN = 1e5 + 5;
const int maxK = 1e2 + 5;
const int inf = 1e18 + 7;
const int mod = 1e9 + 7;
int n, K;
int a[maxN];
struct ST {
int st[maxN * 4];
void upd (int id, int l, int r, int pos, int val) {
if (pos < l || pos > r) {
return;
}
if (l == r) {
st[id] = val;
return;
}
int mid = (l + r) >> 1;
upd (id * 2, l, mid, pos, val);
upd (id * 2 + 1, mid + 1, r, pos, val);
st[id] = max (st[id * 2], st[id * 2 + 1]);
}
int get (int id, int l, int r, int u, int v) {
if (v < l || u > r) {
return 0;
}
if (u <= l && r <= v) {
return st[id];
}
int mid = (l + r) >> 1;
int tL = get (id * 2, l, mid, u, v);
int tR = get (id * 2 + 1, mid + 1, r, u, v);
return max (tL, tR);
}
} T;
namespace sub3 {
int dp[105][105]; // minium splited with k values in current i
// dp i k = max (dp j k - 1 + max (ai -> aj))
void sol () {
for (int i = 1; i <= n; i ++) {
cin >> a[i];
T.upd (1, 1, n, i, a[i]);
}
for (int i = 0; i <= n; i ++) {
for (int k = 1; k <= K; k ++) {
dp[i][k] = inf;
}
}
for (int i = 1; i <= n; i ++) {
dp[i][1] = T.get (1, 1, n, 1, i);
}
for (int i = 1; i <= n; i ++) {
for (int k = 2; k <= min (i, K); k ++) {
for (int j = k; j <= i; j ++) {
//cout << dp[j][k - 1] << ' ' << T.get (1, 1, n, j, i) << ' ' << i << ' ' << j << ' ' << k << '\n';
mini (dp[i][k], dp[j - 1][k - 1] + T.get (1, 1, n, j, i));
}
//cout << i << ' ' << k << ' ' << dp[i][k] << '\n';
}
}
cout << dp[n][K];
}
}
namespace full {
int dp[2][maxN]; // min splited
void sol () {
for (int i = 1; i <= n; i ++) {
cin >> a[i];
}
for (int i = 0; i <= n; i ++) {
dp[0][i] = dp[1][i] = inf;
}
dp[0][0] = 0;
for(int j = 1; j <= K; j ++){
stack <int> st;
int cur = (j & 1);
int prev = 1 - cur;
for (int i = 1; i <= n; i ++){
dp[cur][i] = dp[prev][i - 1] + a[i];
// stack duy tri duyet lai cac doan con ai max
while(!st.empty() && a[st.top ()] <= a[i]){
mini (dp[cur][i], dp[prev][st.top ()] + a[i]); // splited to new block
mini (dp[cur][i], dp[cur][st.top ()] - a[st.top ()] + a[i]); // optimize the cut on top upto i
st.pop();
}
if (!st.empty ()){
mini (dp[cur][i], dp[cur][st.top ()]); // not splited
}
st.push (i);
}
for (int i = 0; i <= n; i++){
dp[prev][i] = inf;
}
}
cout << min (dp[0][n], dp[1][n]);
}
}
void solve () {
cin >> n >> K;
if (n <= 1) {
sub3 :: sol ();
}
else {
full :: sol ();
}
}
signed main () {
cin.tie (nullptr) -> sync_with_stdio (false);
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int t = 1;
//cin >> t;
while (t --) {
solve ();
}
return 0;
}
// thfdgb
Compilation message (stderr)
blocks.cpp: In function 'int main()':
blocks.cpp:136:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:137:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | 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... |