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>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 100'032;
const int K = 110;
const ll inf = 1e15;
ll dp[K][N];
ll a[N];
int n;
__attribute__((optimize("O3,unroll-loops"),target("avx2")))
ll get_min(int k, int l, int r)
{
ll ans = inf;
Loop (i,l,r)
ans = min(ans, dp[k][i]);
return ans;
}
int main()
{
cin.tie(0) -> sync_with_stdio(false);
int k;
cin >> n >> k;
Loop (i,0,n)
cin >> a[i];
dp[1][0] = a[0];
Loop (i,2,k+1)
dp[i][0] = inf;
vector<int> vec;
vec.push_back(0);
Loop (i,1,n) {
while (vec.size() && a[vec.back()] < a[i])
vec.pop_back();
dp[1][i] = max(a[i], dp[1][i-1]);
Loop (j,2,k+1) {
dp[j][i] = get_min(j-1, vec.size()?
vec.back():
0, i) + a[i];
if (vec.size())
dp[j][i] = min(dp[j][i], dp[j][vec.back()]);
}
vec.push_back(i);
}
cout << dp[k][n-1] << '\n';
}
# | 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... |