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;
using namespace std;
#define ll long long
#define vt vector
#define pb push_back
#define pii pair<int, int>
#define sz(dq) (int)dq.size()
#define forr(i, a, b) for(int i = a; i < b; i++)
#define fi first
#define se second
#define pll pair<ll, ll>
ifstream fin("store.inp");
ofstream fout("store.out");
const ll mxn = 1e5 + 5, inf = 1e9, mod = 1e9 + 7, sq = 800, mxv = 1e6 + 5, pr = 37, mod2 = 1e9 + 9, mod3 = 998244353;
//const int x[4] = {0, -1, 0, 1};
int n, k;
int a[mxn + 1];
ll dp[mxn + 1][105];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++)cin >> a[i];
for(int i = 0; i <= n; i++){
for(int j = 0; j <= k; j++){
dp[i][j] = inf;
}
}
dp[0][0] = 0;
for(int i = 1; i <= k; i++){
deque<pair<int, ll>>dq;
for(int j = 1; j <= n; j++){
ll mn = dp[j - 1][i - 1];
while(sz(dq) && dq.back().first <= a[j]){
mn = min(mn, dq.back().second);
dq.pop_back();
}
dq.pb(make_pair(a[j], mn));
while(sz(dq) >= 2 && dq.back().first + dq.back().second >= dq[sz(dq) - 2].second + dq[sz(dq) - 2].second)dq.pop_back();
dp[j][i] = dq.back().first + dq.back().second;
//cout << dp[j][i] << " ";
}
}
cout << dp[n][k];
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... |