제출 #742476

#제출 시각아이디문제언어결과실행 시간메모리
742476hmm789Tortoise (CEOI21_tortoise)C++14
18 / 100
53 ms3256 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1000000000000000000

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n, ans = INF;
	cin >> n;
	int a[n], lt[n], rt[n], pref[n];
	pair<int, int> dp[2*n+1][n];
	for(int i = 0; i < n; i++) cin >> a[i];
	lt[0] = a[0]==-1?0:INF;
	for(int i = 1; i < n; i++) {
		lt[i] = lt[i-1];
		if(a[i] == -1) lt[i] = i;
	}
	rt[n-1] = a[n-1]==-1?n-1:INF;
	for(int i = n-2; i >= 0; i--) {
		rt[i] = rt[i+1];
		if(a[i] == -1) rt[i] = i;
	}
	pref[0] = max(a[0], 0LL);
	for(int i = 1; i < n; i++) pref[i] = pref[i-1]+max(a[i], 0LL);
	for(int i = 0; i <= 2*n; i++) {
		for(int j = 0; j < n; j++) {
			dp[i][j].first = INF;
			dp[i][j].second = 0;
		}
	}
	for(int i = 0; i <= 2*n; i++) {
		for(int j = 0; j < n; j++) {
			if(a[j] <= 0) continue;
			if(i < j) continue;
			if(max(pref[j]-1, 0LL) < dp[i][j].first) {
				dp[i][j] = {max(pref[j]-1, 0LL), 1};
			}
			int sm = 0;
			for(int k = j; k < n; k++) {
				if(k > j) sm += max(a[k], 0LL);
				if(a[k] <= 0) continue;
				int dist = min(abs(j-lt[j])+abs(k-lt[j]), abs(rt[j]-j)+abs(rt[j]-k));
				//~ cout << "b " << i << " " << j << " " << k << " " << dist+i << endl;
				if(dist+i > 2*n) continue;
				if(dist+i > 2*k) continue;
				if(k == j) {
					if(dp[i][j].second < a[k] && dp[dist+i][k].first > max(dp[i][j].first-1, 0LL)) {
						dp[dist+i][k] = {max(dp[i][j].first-1, 0LL), dp[i][j].second+1};
						//~ cout << dp[dist+i][k].first << endl;
					}
				} else {
					//~ cout << "b " << dp[dist+i][k].first<< " " <<  dp[i][j].first+max(sm-1, 0LL) << endl;
					if(dp[dist+i][k].first > dp[i][j].first+max(sm-1, 0LL)) {
						dp[dist+i][k] = {dp[i][j].first+max(sm-1, 0LL), 1};
						//~ cout << dp[dist+i][k].first << endl;
					}
				}
				//~ cout << "a " << i << " " << j << " " << k << " " << dist+i << " " << dp[dist+i][k].first << " " << dp[i][j].first << " " << sm << endl;
			}
			ans = min(ans, dp[i][j].first+pref[n-1]-pref[j]);
		}
	}
	cout << ans;
	
	//~ cout << endl;
	//~ for(int i = 0; i <= 2*n; i++) {
		//~ for(int j = 0; j < n; j++) {
			//~ cout << dp[i][j].first << " ";
		//~ }
		//~ cout << endl;
	//~ }
	
	//~ cout << endl;
	//~ for(int i = 0; i < n; i++) cout << lt[i] << " ";
	//~ cout << endl;
	//~ for(int i = 0; i < n; i++) cout << rt[i] << " ";
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...