# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1116996 | whoknow | K blocks (IZhO14_blocks) | C++17 | 149 ms | 3536 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 int 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;
const ll INF = 1e18;
int n, K;
int a[MAXN];
namespace sub1
{
int dp[2][MAXN];
void solve()
{
for(int i = 1; i <= n; i++)
dp[0][i] = max(dp[0][i - 1], a[i]);
for(int j = 2; j <= K; j++)
{
stack<ii>st;
for(int i = j; i <= n; i++)
{
int tmp = dp[0][i - 1];
while(sz(st) && st.top().S < a[i])
{
tmp = min(tmp, st.top().F);
st.pop();
}
if(sz(st) == 0 || tmp + a[i] <= st.top().F + st.top().S)
st.push({tmp,a[i]});
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()
{
if(fopen("TEST.inp", "r"))
{
freopen("TEST.inp", "r", stdin);
freopen("TEST.out", "w", stdout);
}
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... |