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
#include "kanapka.h"
#include "message.h"

#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

long long* sum_r = NULL;
long long* best_sum_r = NULL;

int start_idx = 0;
int chunk_count = 0;

int ChunkIdx(int i) {
	return start_idx + i;
}

int Max4(long long a, long long b,
				 long long c, long long d) {
	return max(max(max(a, b), c), d);
}

long long ComputeLeft(
		long long start_sum,
		long long start_best_sum,
		long long* const sum_o,
		long long* const best_sum_o) {
	long long result = 0;
	long long sum = start_sum + GetTaste(ChunkIdx(0));
	long long best_sum = max(start_best_sum, sum);
	for (int i = 1; i < chunk_count; i++) {
		long long taste = GetTaste(ChunkIdx(i));

//		fprintf(stderr, "r: %lld, bs: %lld, bsr: %lld, s: %lld, sr: %lld, t: %lld\n",
//					  result, best_sum, best_sum_r[i + 1],
//					  sum, sum_r[i + 1], taste);

		result = Max4(result,
									best_sum + best_sum_r[i + 1],
									best_sum + (taste + sum_r[i + 1]),
									(sum + taste) + best_sum_r[i + 1]);
		
		sum += taste;
		best_sum = max(best_sum, sum);

//		fprintf(stderr, "new | r: %lld, s: %lld, bs: %lld\n",
//					  result, sum, best_sum);
	}
	if (sum_o) *sum_o = sum;
	if (best_sum_o) *best_sum_o = best_sum;

	return result;
}

void ComputeRight(
		long long* const sum_o,
		long long* const best_sum_o) {
	sum_r[chunk_count] = 0;
	best_sum_r[chunk_count] = 0;
	for (int i = chunk_count - 1; i >= 0; i--) {
		sum_r[i] = sum_r[i + 1] + GetTaste(ChunkIdx(i));
		best_sum_r[i] = max(best_sum_r[i + 1], sum_r[i]);
	}
	if (sum_o) *sum_o = sum_r[0];
	if (best_sum_o) *best_sum_o = best_sum_r[0];
}

long long SolveLocally() {
	long long result = 0;
	if (chunk_count == 1) {
		result = max(0LL, GetTaste(0));
	}
	else if (chunk_count == 2) {
		result = Max4(0LL,
						  		GetTaste(0),
							  	GetTaste(1),
							    GetTaste(0) + GetTaste(1));
	} else {
		ComputeRight(NULL, NULL);
		result = ComputeLeft(0, 0, NULL, NULL);
	}

	printf("%lld", result);
}

void SendLL(int target, long long one, long long two) {
	PutLL(target, one);
	PutLL(target, two);
	Send(target);
}

void ReceiveLL(int target, long long* const one, long long* const two) {
	assert(Receive(target) >= 0);
	*one = GetLL(target);
	*two = GetLL(target);
}

void SolveDist(int node_id, int nodes) {
//	fprintf(stderr, "node: %d/%d -- start_idx: %d, count: %d\n", node_id, nodes, start_idx, chunk_count);

	long long my_sum_r;
	long long my_best_sum_r;
	long long start_sum_r = 0;
	long long start_best_sum_r = 0;
	
	ComputeRight(&my_sum_r, &my_best_sum_r);
	if (node_id < nodes - 1) {
		ReceiveLL(node_id + 1, &start_sum_r, &start_best_sum_r);
		my_best_sum_r += start_sum_r;
	}
	if (node_id > 0) {
		SendLL(node_id - 1, start_sum_r + my_sum_r,
											  max(start_best_sum_r, my_best_sum_r));
	}
//	fprintf(stderr, "my_sum_r: %lld my_best_sum_r: %lld start_sum_r: %lld start_best_sum_r: %lld\n",
//				  my_sum_r, my_best_sum_r, start_sum_r, start_best_sum_r);
	for (int i = 0; i <= chunk_count; ++i) {
		sum_r[i] += start_sum_r;
		best_sum_r[i] += start_sum_r;
	}

	long long my_sum_l;
	long long my_best_sum_l;
	long long start_sum_l = 0;
	long long start_best_sum_l = 0;

	assert(ComputeLeft(0, 0, &my_sum_l, &my_best_sum_l) >= 0);
	if (node_id > 0) {
		ReceiveLL(node_id - 1, &start_sum_l, &start_best_sum_l);
		my_best_sum_l += start_sum_l;
	}
	if (node_id < nodes - 1) {
		SendLL(node_id + 1, start_sum_l + my_sum_l,
												max(start_best_sum_l, my_best_sum_l));
	}
//	fprintf(stderr, "my_sum_l: %lld, my_best_sum_l: %lld, start_sum_l: %lld, start_best_sum_l: %lld\n",
//				  my_sum_l, my_best_sum_l, start_sum_l, start_best_sum_l);

	long long my_result = ComputeLeft(start_sum_l, start_best_sum_l, NULL, NULL);
//  fprintf(stderr, "my_result: %lld\n", my_result);
	
	if (node_id == 0) {
		for (int i = 1; i < nodes; ++i) {
			long long other_result, ignore;
			ReceiveLL(i, &other_result, &ignore);
			my_result = max(my_result, other_result);
		}
		printf("%lld\n", my_result);
	} else {
		SendLL(0, my_result, 0);
	}
}

void Init(int chunks) {
	sum_r = new long long[chunks + 2];
	best_sum_r = new long long[chunks + 2];
}

int main() {
	const int n = GetN();
	const int node_id = MyNodeId();
	const int nodes = NumberOfNodes();

//  fprintf(stderr, "n: %d, id: %d, nodes: %d\n", n, node_id, nodes);

	if (n < 1000000) {
		if (node_id == 0) {
			start_idx = 0;
			chunk_count = n;
			Init(chunk_count);
			SolveLocally();
		}
	} else {
		const int chunks_per_node = ceil((double)n / nodes);
		start_idx = node_id * chunks_per_node;
		const int end_idx = min((node_id + 1) * chunks_per_node, n);
		chunk_count = end_idx - start_idx;
		Init(chunk_count);
		SolveDist(node_id, nodes);
	}

	return 0;
}