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;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define FF first
#define SS second
#define pb push_back
#define sz(x) (int)x.size()
#define oo 1e9
#define PI acos(-1.0)
#define lb lower_bound
#define ub upper_bound
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
const int N = 100000+7, M=100+7;
const ll MOD = 1e9+7;
int n, m, k, t;
int a[N], _dp[N][M];
signed main() {
//freopen("test.inp", "r", stdin);
//freopen("test.out", "w", stdout);
//fastIO;
scanf("%d%d", &n, &m);
for(int i=1;i<=n;scanf("%d", &a[i++]))
for(int j=0;j<=m;j++) _dp[i][j]=oo;
for(int i=0;i<=m;i++) _dp[0][i]=oo;_dp[0][1]=0;
for(int i=1;i<=n;i++) _dp[i][1]=max(_dp[i-1][1], a[i]);
for(int mn, j=2;j<=m;j++) {
stack<ii> st;
for(int i=j;i<=n;i++) {
mn=_dp[i-1][j-1];
while(sz(st)&&a[st.top().SS]<=a[i])
mn=min(st.top().FF, mn), st.pop();
_dp[i][j]=min(_dp[sz(st)?st.top().SS:0][j], mn+a[i]);
st.push(ii(mn, i));
}
}
printf("%d", _dp[n][m]);
return 0;
}
/* stuff you should look for
- int overflow, array bounds
- special cases (n=1?)
- do smth instead of do nothing and stay organized
- WRITE STUFF DOWN
- DONT JUST STICK ON ONE APPROACH
*/
Compilation message (stderr)
blocks.cpp: In function 'int main()':
blocks.cpp:29:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
29 | for(int i=0;i<=m;i++) _dp[0][i]=oo;_dp[0][1]=0;
| ^~~
blocks.cpp:29:40: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
29 | for(int i=0;i<=m;i++) _dp[0][i]=oo;_dp[0][1]=0;
| ^~~
blocks.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
blocks.cpp:27:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | for(int i=1;i<=n;scanf("%d", &a[i++]))
| ~~~~~^~~~~~~~~~~~~~~
# | 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... |