#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(I, L, R) for(int I(L); I <= (int)R ; ++I)
#define FOD(I, R, L) for(int I(R); I >= (int)L ; --I)
#define FOA(I, A) for(auto &I : A)
#define print(A, L, R) FOR(I, L, R){cout << A[I] <<' ';} cout << '\n';
#define printz(A, L, R) FOR(I, 0, L){FOR(J, 0, R){cout << A[I][J] << ' ';} cout << '\n';} cout << '\n';
#define prints(A) FOA(I, A) cout << I <<' '; cout << '\n';
#define fs first
#define sd second
#define ii pair<int, int>
#define iii pair<int, ii>
#define all(A) A.begin(), A.end()
#define quickly cin.tie(0) -> ios_base::sync_with_stdio(0);
#define FILE "dincpath"
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int oo = 1e18;
struct Modint{
int x;
Modint(){x = 0;}
Modint(int _x){x = (_x + mod) % mod;}
Modint operator + (const Modint &other) const{ return Modint((x + other.x) % mod); }
Modint operator - (const Modint &other) const{ return Modint((x - other.x + mod) % mod); }
Modint operator * (const Modint &other) const{ return Modint((1LL * x * other.x) % mod); }
void operator += (const Modint &other) { *this = *this + other; }
void operator -= (const Modint &other) { *this = *this - other; }
void operator *= (const Modint &other) { *this = *this * other; }
friend ostream& operator << (ostream& os, const Modint &other){
return os << other.x;
}
};
int n, k;
int a[N], dp[2][N];
int rmq[N][20];
void BuildRMQ(){
FOR(i, 1, n){
rmq[i][0] = a[i];
}
for(int j(1) ; (1 << j) <= n ; ++j){
FOR(i, 1, n - (1 << j) + 1){
rmq[i][j] = max(rmq[i][j - 1], rmq[i + (1 << j - 1)][j - 1]);
}
}
}
int f(int l, int r){
if(l > r) return oo;
int k = __lg(r - l + 1);
return max(rmq[l][k], rmq[r - (1 << k) + 1][k]);
}
void dnc(int l, int r, int optl, int optr){
if(l > r) return;
int mid = (l + r) >> 1;
ii best = ii(oo, 0);
FOR(i, optl, min(mid, optr)){
best = min(best, ii(dp[0][i] + f(i + 1, mid), i));
}
dp[1][mid] = best.fs;
int opt = best.sd;
dnc(l, mid - 1, optl, opt);
dnc(mid + 1, r, opt, optr);
}
signed main(){ quickly
if(fopen(FILE".inp", "r")){
freopen(FILE".inp", "r", stdin);
freopen(FILE".out", "w", stdout);
}
cin >> n >> k;
FOR(i, 1, n){
cin >> a[i];
}
BuildRMQ();
memset(dp, 0x3f, sizeof dp);
dp[0][0] = 0;
FOR(i, 1, k){
dnc(0, n, 0, n);
FOR(j, 0, n){
dp[0][j] = dp[1][j];
dp[1][j] = oo;
}
}
cout << dp[0][n];
}
Compilation message (stderr)
blocks.cpp: In function 'int main()':
blocks.cpp:88:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen(FILE".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:89:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen(FILE".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... |