# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1116971 | whoknow | K blocks (IZhO14_blocks) | C++17 | 1 ms | 336 KiB |
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 ll long long
#define F first
#define S second
#define MAXN 100005
#define ii pair<int,int>
#define bit(i,j) ((i>>j)&1)
#define sz(i) (int)i.size()
#define endl '\n'
using namespace std;
int n, K;
int a[MAXN];
namespace sub1
{
ll dp[2][MAXN];
void solve()
{
int mi = 1e9;
for(int i = 1; i <= n; i++)
{
mi = min(mi, a[i]);
dp[0][i] = mi;
}
for(int j = 2; j <= K; j++)
{
stack<ii>st;
for(int i = 1; i <= n; i++)
{
while(sz(st) && st.top().S < a[i])
st.pop();
if(sz(st) == 0 || dp[0][i] + a[i] <= st.top().F + st.top().S)
st.push({dp[0][i], a[i]});
if(i > j)
dp[1][i] = st.top().F + st.top().S;
}
for(int i = 1; i <= n; i++)
dp[0][i] = dp[1][i];
}
cout<<dp[0][n];
}
}
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> K;
for(int i = 1; i <= n; i++)
cin >> a[i];
sub1::solve();
}
Compilation message (stderr)
# | 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... |