Submission #331072

# Submission time Handle Problem Language Result Execution time Memory
331072 2020-11-27T08:51:34 Z Gurban K blocks (IZhO14_blocks) C++17
0 / 100
1 ms 384 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int N = 1e2+5;
const int maxn=1e5+5;
int n,k,a[maxn];
ll dp[N][maxn];
stack<pair<int,ll>>s;

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

	cin >> n >> k;
	for(int i = 1;i <= n;i++) cin >> a[i];
	
	for(int i = 1;i <= k;i++){
		dp[i][i] = dp[i-1][i-1]+a[i];
		s.push({i,dp[i][i]});
		for(int j = i+1;j <= n;j++){
			while(!s.empty() and a[s.top().first] <= a[j]) s.pop();
			if(!s.empty()){
				ll nw = a[j] + dp[i-1][s.top().first];
				s.push({j,min(nw,s.top().second)});
			}
			else s.push({j,dp[i-1][i-1]+a[j]});
			dp[i][j] = s.top().second;
		}
	}
	cout<<dp[k][n];
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 0 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -