1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a); i<(b); ++i)
typedef vector<int> vi;
#define pb push_back
#define sz size()
typedef pair<int,int> pii;
#define mp make_pair
#define st first
#define nd second
typedef long long ll;
#define INF 1000000001
#define ALL(t) t.begin(),t.end()
#define SC(a) scanf("%d", &a)
#define GET(a) int a; SC(a)

#define ISDEBUG 0
#define dprintf(...) if(ISDEBUG) \
{printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");}
template <class It> void dptab(It b, It e, const char* f="%d ") {
	if(ISDEBUG) {
		for(It it=b; it!=e; ++it) dprintf(f, *it); dprintf("\n");
}}

struct node {
	int prev;
	int next;
	int prev_dist;
	int next_dist;
	int energy;
};

vector<node> line;

int find_most_left(int factory) {
	int energy = line[factory].energy;
	int i = factory;
	do {
		if (line[i].prev == -1) {
			return i;
		} else {
			i = line[i].prev;
			energy += line[i].energy;
		}
	} while(energy < 0);
	return i;
}

int find_most_right(int factory) {
	int energy = line[factory].energy;
	int i = factory;
	do {
		if (line[i].next == -1) {
			return i;
		} else {
			i = line[i].next;
			if (line[i].energy > 0)
				energy += line[i].energy;
		}
	} while(energy < 0);
	return i;
}

int find_right(int left, int factory, int& right, int& energy_sum) {
	dprintf("find right for left #%d\n", left);
	int energy_gain = 0;
	int distance = 0;
	int i = factory;
	while(i != -1 && i >= left) {
		energy_gain += line[i].energy;
		if (i != left) {
			distance += line[i].prev_dist;
		}
		i = line[i].prev;
	}
	energy_sum = energy_gain;
	right = factory;
	while(energy_gain < 0) {
		right = line[right].next;
		if (right == -1) return -1;
		energy_sum += line[right].energy;
		if(line[right].energy > 0)
			energy_gain += line[right].energy;
		distance += line[right].prev_dist;
	}
	dprintf("found #%d! distance = %d\n", right, distance);
	return distance;
}

int find_solution(int factory, int& best_left, int& best_right, int& energy) {
	dprintf("find solution for factory #%d\n", factory);
	int best_result = INF;
	int most_left, most_right;
	most_left = find_most_left(factory);
	//most_right = find_most_right(factory);
	dprintf("most left = %d\n", most_left);
	//dprintf("most right = %d\n", most_right);

	for(int i=most_left; i<=factory && i!=-1;) {
		int right, energy_sum;
		int distance = find_right(i, factory, right, energy_sum);
		if (-1 != distance && distance < best_result) {
			best_result = distance;
			best_left = i;
			best_right = right;
			energy = energy_sum;
		}
		i = line[i].next;
	}
	dprintf("best result found: %d\n", best_result);

	if (best_result == INF) return -1;
	else return best_result;
}

int reduce(int i, int energy, int left, int right) {
	line[i].energy = energy;

	if(left != i) {
		line[i].prev = line[left].prev;
		line[i].prev_dist = line[left].prev_dist;

		int prev = line[left].prev;
		if (prev != -1) {
			line[prev].next = i;
		}
	}
	if(right != i) {
		line[i].next = line[right].next;
		line[i].next_dist = line[right].next_dist;

		int next = line[right].next;
		if(next != -1) {
			line[next].prev = i;
		}
	}
	dprintf("reduce return %d with energy = %d\n", i, line[i].energy);
	return i;
}

int skip_zero(int i) {
	int prev = line[i].prev;
	int next = line[i].next;
	if(prev != -1) {
		line[prev].next = next;
		line[prev].next_dist = min(INF, line[i].next_dist + line[i].prev_dist);
	}
	if(next != -1) {
		line[next].prev = prev;
		line[next].prev_dist = min(INF, line[i].next_dist + line[i].prev_dist);
	}
	return next;
}

int main() {
	GET(n);
	FOR(i,0,n) {
		GET(energy);
		line.pb({i-1, i+1, 1, 1, energy});
	}
	line[0].prev_dist = INF;
	line[n-1].next = -1;
	line[n-1].next_dist = INF;

	int overall_cost = 0;

	for(int i=0; i!=-1; ) {
		if(line[i].energy==0) {
			dprintf("skipping zero at %d\n", i);
			i = skip_zero(i);
		} else {
			i = line[i].next;
		}
	}

	for(int i=0; i!=-1; ) {
		dprintf("going for i = %d\n", i);
		if (line[i].energy < 0) {
			int left, right, energy;
			int cost = find_solution(i, left, right, energy);
			if(cost == -1) {
				dprintf("============\n");
				printf("%d\n", -1);
				return 0;
			}
			overall_cost += cost;
			i = reduce(i, energy, left, right);
			dprintf("new i = %d", i);
		} else if (line[i].energy > 0) {
			dprintf("skipping ele at %d\n", i);
			i = line[i].next;
		} else {
			dprintf("skipping zero at %d\n", i);
			i = skip_zero(i);
		}
		dprintf("new i = %d\n", i);
	}

	printf("%d\n", overall_cost);
		if(ISDEBUG) {
			for(int d=0;d<line.sz;++d) {
				dprintf("#%d: %d (dist %d) <- %d -> %d (dist %d)\n",
					d,
					line[d].prev,
					line[d].prev_dist,
					line[d].energy,
					line[d].next,
					line[d].next_dist
					);
			}
			dprintf("======================================\n");
		}

	return 0;
}