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 fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 1e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;
int n, k, a[N];
namespace Subtask1{
bool ckSub(){
return n <= 1e2;
}
const int N = 105, K = 105;
int dp[K][N];
void process()
{
FOR(i, 0, k){
FOR(j, 0, n){
dp[i][j] = 1e9 + 1;
}
}
dp[0][0] = 0;
FOR(i, 1, k){
FOR(r, 1, n){
int Mx = a[r];
for(int l = i; l >= 1; l--){
ckmx(Mx, a[l]);
ckmn(dp[i][r], dp[i-1][l-1] + Mx);
}
}
}
cout << dp[k][n] <<"\n";
return;
}
}
namespace Subtask2{
bool ckSub(){
return n <= N;
}
const int K = 105;
ll dp[2][N];
void compute(int l, int r, int optL, int optR){
if(l > r) return;
int mid = l+r>>1;
int best = 0; ll last = INF, Mx = a[min(mid, optR)];
for(int i = min(mid, optR); i >= optL; i--){
ckmx(Mx, 1ll * a[i]);
if(ckmn(last, dp[0][i-1] + Mx)){
best = i;
}
}
dp[1][mid] = last;
compute(l, mid-1, optL, best);
compute(mid+1, r, best, optR);
return;
}
void process()
{
FOR(i, 0, 1){
FOR(j, 0, n){
dp[i][j] = INF;
}
}
dp[0][0] = 0;
FOR(i, 1, k){
compute(1, n, 1, n);
swap(dp[0], dp[1]);
dp[0][0] = INF;
}
cout << dp[0][n] <<"\n";
return;
}
}
void solve()
{
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
Subtask2::process(); return;
if(Subtask1::ckSub()){
return Subtask1::process(), void();
}
else{
return Subtask2::process(), void();
}
return;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
Compilation message (stderr)
blocks.cpp: In function 'void Subtask2::compute(int, int, int, int)':
blocks.cpp:76:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
76 | int mid = l+r>>1;
| ~^~
blocks.cpp: In function 'int main()':
blocks.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | freopen(name".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |