Submission #859872

#TimeUsernameProblemLanguageResultExecution timeMemory
859872OAleksaK blocks (IZhO14_blocks)C++14
53 / 100
1057 ms12892 KiB
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
#define int long long
const int maxn = 1e5 + 69;
const int maxk = 110;
const int lg = 17;
const int inf = 1e18;
int a[maxn], n, k, dp[maxn][maxk], st[maxn][lg];
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int tt = 1;
	//cin >> tt;
	while(tt--) {
		cin >> n >> k;
		for (int i = 1;i <= n;i++)
			cin >> a[i];
		for (int i = 1;i <= n;i++)
			st[i][0] = a[i];
		for (int i = 0;i <= n;i++) {
			for (int j = 0;j <= k;j++)
				dp[i][j] = inf;
		}
		dp[0][0] = 0;
		for (int j = 1;j < lg;j++)
			for (int i = 1;i + (1 << j) <= n + 1;i++)
				st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
		for (int i = 1;i <= n;i++) {
			for (int j = 1;j <= k;j++) {
				for (int x = i - 1;x >= 0;x--) {
					int q = log2(i - x);
					int mx = max(st[x + 1][q], st[i - (1 << q) + 1][q]);
					dp[i][j] = min(dp[i][j], mx + dp[x][j - 1]);
				}
			}
		}
		cout << dp[n][k];
	}	
   return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...