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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ft first
#define sc second
using namespace std;
const long long mod=1e9+7,INF=1e17;
int a[1000005],dp[105][1000005];
main(){
int n,k,mx=0;
cin >> n >> k;
for(int i=1;i<=n;i++){
cin >> a[i];
}
for(int i=1;i<=n;i++){
mx=max(mx,a[i]);
dp[1][i]=mx;
}
for(int i=2;i<=k;i++){
vector<pair<int,int>> v;
for(int j=i;j<=n;j++){
int tmp=dp[i-1][j-1];
while(!v.empty() && v.back().ft<a[j]){
tmp=max(tmp,v.back().sc);
v.pop_back();
}
if(v.empty()|| v.back().ft+v.back().sc>(tmp+a[j])){
v.push_back({a[i],tmp});
}
dp[i][j]=v.back().ft+v.back().sc;
}
}
cout << dp[k][n];
}
Compilation message (stderr)
blocks.cpp:12:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
12 | main(){
| ^~~~
# | 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... |