이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n"
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
* SIMPLIFY THE PROBLEM
* READ THE STATEMENT CAREFULLY
!!! if there is an specified/interesting smth(i.e. constraint) in the statement,
then you must be careful about that
*/
const int INF = 1e18;
void solve(){
int n,k;cin >> n >> k;
int ar[n+5];
for(int i=1;i<=n;i++) cin >> ar[i];
int dp[n+5][k+5];
for(int i=0;i<=n;i++) for(int j=0;j<=k;j++) dp[i][j]=INF;
for(int i=1;i<=n;i++) dp[i][1]=max((i>1 ? dp[i-1][1] : 0),ar[i]);
for(int j=2;j<=k;j++){
stack<array<int,2>> s;
for(int i=1;i<=n;i++){
int cur=dp[i-1][j-1];
while(!s.empty() && s.top()[0]<=ar[i]){
cur=min(cur,s.top()[1]);
s.pop();
}
if(s.empty() || ar[i]+cur<s.top()[0]+s.top()[1]) s.push({ar[i],cur});
dp[i][j]=s.top()[0]+s.top()[1];
}
}
cout << dp[n][k] << endl;
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int t=1;//cin >> t;
while(t--) solve();
return 0;
}
# | 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... |