제출 #53403

#제출 시각아이디문제언어결과실행 시간메모리
53403MatheusLealVK blocks (IZhO14_blocks)C++17
100 / 100
531 ms99940 KiB
#include <bits/stdc++.h>
#define N 100010
#define K 110
#define inf 200000000000000000LL
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;

ll n, k, v[N], dp[N][K], maior;

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);

	cin>>n>>k;

	for(int i = 1; i <= n; i++) cin>>v[i];

	for(int i = 0; i < N; i++)
		for(int j = 0; j < K; j++)
			dp[i][j] = inf;

	for(int i = 1; i <= n; i++)
	{
		maior = max(maior, v[i]);

		dp[i][1] = maior;
	}

	for(int q = 2; q <= k; q++)
	{
		stack < pii > W;

		for(int i = 1; i <= n; i++)
		{
			ll opt = dp[i - 1][q - 1];

			while(!W.empty() and W.top().f < v[i])
			{
				opt = min(opt, W.top().s);

				W.pop();
			}

			if(W.empty() or opt + v[i] < W.top().f + W.top().s) W.push({v[i], opt});

			if(i >= q) dp[i][q] = W.top().f + W.top().s;
		}
	}

	cout<<dp[n][k]<<'\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...