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>
using namespace std;
const int mod = 1e9 + 7;
const auto seed = chrono::steady_clock::now().time_since_epoch().count();
mt19937_64 ran(seed);
#define ll long long
#define int long long
#define se second
#define fi first
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define REP(i, a, b) for (int i = (a), _b = (b); i < _b; i++)
#define FOD(i,r,l) for(int i=r; i>=l; i--)
int n, k;
int a[200000];
int f[100004][102];
signed main(){
#define task "a"
if(fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> k;
memset(f, 0x3f, sizeof f);
f[0][1] = 0 ;
FOR(i ,1, n) cin >> a[i], f[i][1] = max(f[i-1][1], a[i]);
FOR(j ,2, k){
stack <pair <int,int>> st;
FOR(i ,1, n){
int mn = 1e9;
while(st.size() && a[st.top().fi] < a[i]){
mn = min(mn, st.top().se);
st.pop();
}
f[i][j] = min(f[i][j], mn + a[i]);
if(st.size()) f[i][j] = min(f[i][j], st.top().se + a[st.top().fi + 1]);
st.push(make_pair(i, f[i][j-1]));
}
}
cout << f[n][k];
}
Compilation message (stderr)
blocks.cpp: In function 'int main()':
blocks.cpp:24:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:25:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | 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... |