#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(I, L, R) for(int I(L); I <= (int)R ; ++I)
#define FOD(I, R, L) for(int I(R); I >= (int)L ; --I)
#define FOA(I, A) for(auto &I : A)
#define print(A, L, R) FOR(I, L, R){cout << A[I] <<' ';} cout << '\n';
#define printz(A, L, R) FOR(I, 0, L){FOR(J, 0, R){if(A[I][J] >= oo / 10){cout << "- ";} else cout << A[I][J] << ' ';} cout << '\n';} cout << '\n';
#define prints(A) FOA(I, A) cout << I <<' '; cout << '\n';
#define fs first
#define sd second
#define ii pair<int, int>
#define iii pair<int, ii>
#define all(A) A.begin(), A.end()
#define quickly cin.tie(0) -> ios_base::sync_with_stdio(0);
#define FILE "dincpath"
const int N = 1e5 + 5;
const int K = 105;
const int mod = 1e9 + 7;
const int oo = 1e18;
struct Modint{
int x;
Modint(){x = 0;}
Modint(int _x){x = (_x + mod) % mod;}
Modint operator + (const Modint &other) const{ return Modint((x + other.x) % mod); }
Modint operator - (const Modint &other) const{ return Modint((x - other.x + mod) % mod); }
Modint operator * (const Modint &other) const{ return Modint((1LL * x * other.x) % mod); }
void operator += (const Modint &other) { *this = *this + other; }
void operator -= (const Modint &other) { *this = *this - other; }
void operator *= (const Modint &other) { *this = *this * other; }
friend ostream& operator << (ostream& os, const Modint &other){
return os << other.x;
}
};
int n, k;
int dp[K][N], opt[K][N], a[N];
stack<int> st;
signed main(){ quickly
if(fopen(FILE".inp", "r")){
freopen(FILE".inp", "r", stdin);
freopen(FILE".out", "w", stdout);
}
cin >> n >> k;
FOR(i, 1, n){
cin >> a[i];
}
memset(dp, 0x3f, sizeof dp);
memset(opt, 0x3f, sizeof opt);
dp[0][0] = 0, opt[1][0] = 0;
FOR(j, 1, k){
st = stack<int>();
st.push(0);
FOR(i, 1, n){
int best = oo;
while(!st.empty() && a[st.top()] <= a[i]){
best = min(best, opt[j][st.top()]);
st.pop();
}
dp[j][i] = best + a[i];
if(!st.empty()){
dp[j][i] = min({dp[j][i], dp[j - 1][st.top()], dp[j][st.top()]});
}
opt[j][i] = min(best, dp[j - 1][i]);
st.push(i);
}
}
cout << dp[k][n];
}
Compilation message (stderr)
blocks.cpp: In function 'int main()':
blocks.cpp:54:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | freopen(FILE".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:55:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | freopen(FILE".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... |