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;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define pb push_back
#define mp make_pair
int n, k;
vector<ll> arr;
ll dp[110][110];
// Example Max SparseTable<int> st(a, [](int i, int j) {return max(i, j);});
// a is the array
template <class T, class F = function<T(const T &, const T &)>>
struct SparseTable {
vector<vector<T>> t;
F func;
SparseTable(const vector<T> &a, F f) : t(32 - __builtin_clz(a.size())), func(std::move(f)) {
t[0] = a;
for (size_t i = 1; i < t.size(); i++) {
t[i].resize(a.size() - (1 << i) + 1);
for (size_t j = 0; j < t[i].size(); j++)
t[i][j] = func(t[i - 1][j], t[i - 1][j + (1 << (i - 1))]);
}
}
T get(int from, int to) const {
assert(0 <= from && from <= to && to <= (int)t[0].size() - 1);
int k = 31 - __builtin_clz(to - from + 1);
return func(t[k][from], t[k][to - (1 << k) + 1]);
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
arr.resize(n);
for(int i = 0; i < n; i++) cin >> arr[i];
ll mx = 0;
for(int i = 0; i < n; i++) for(int j = 0; j < 100; j++) dp[i][j] = 1e18;
for(int i = 0; i < n; i++){
mx = max(mx, arr[i]);
dp[i][1] = mx;
}
SparseTable<ll> st(arr, [](ll i, ll j){return max(i, j);});
for(int splits = 2; splits <= k; splits ++ ){
for(int i = splits-1; i < n; i++){
for(int j = i; j >= splits-1; j--){
ll maxi = st.get(j, i);
dp[i][splits] = min(dp[i][splits], maxi + dp[j-1][splits-1]);
}
}
}
/*for(int i = 1; i < n; i++){
for(int j = i; j >= 0; j--){
for(int splits = 2; splits <= k; splits++){
if(j + 1 < splits) continue;
ll maxi = st.get(j, i);
dp[i][splits] = min(dp[i][splits], maxi + dp[j-1][splits-1]);
}
}
}*/
cout<<dp[n-1][k] << endl;
}
# | 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... |