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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <stdio.h>
#include "message.h"
#include "kanapka.h"
#include <vector>


#define MAX_INT 1000000000

long long int max(long long int a, long long int b){
	return a > b ? a : b;
}

long long int gBegin;
long long int gEnd;
long long int best;
long long int bestPos;
long long int bestRev;
long long int bestRevPos;
long long int shift;
long long int shiftRev;
long long int bestGlobalVal;
long long int bestGlobalRevVal;
//std::vector<long long int> accValues;
long long int lastAccValue;
//std::vector<long long int> accMaxValues;
long long int lastAccRevValue;
//std::vector<long long int> accValuesRev;
//std::vector<long long int> accMaxValuesRev;

void loadData(){
	long long int acc = 0;
	best = -MAX_INT;
	for(int i = gBegin; i < gEnd; i++){
		acc +=  GetTaste(i);
		//accValues.push_back(acc);
//		accMaxValues.push_back(max(best,0));
		//best = max(acc, best);
		if (best < acc){
			best = acc;
			bestPos = i;
		}

	}
	lastAccValue = acc;
}

void loadRevData(){
	long long int acc = 0;
	bestRev = -MAX_INT;
	for(int i = gEnd - 1; i >= gBegin; i--){
		acc += GetTaste(i);
		//accValuesRev.push_back(acc);
//		accMaxValuesRev.push_back(max(bestRev,0));
		//bestRev = max(acc, bestRev);
		if (bestRev < acc){
			bestRev = acc;
			bestRevPos = i;
		}

	}
	lastAccRevValue = acc;
}

void sendData(int id, int totalNodes){
	for(int i = id + 1; i < totalNodes; i++){
		PutLL(i, lastAccValue);
		//PutLL(i, accMaxValues.back());
		Send(i);
	}

	for(int i = id - 1; i >= 0 ; i--){
		PutLL(i, lastAccRevValue);
		//PutLL(i, accMaxValuesRev.back());
		Send(i);
	}
}

void getData(int id, int totalNodes){
	shift = 0;
	shiftRev = 0;
	//bestGlobalVal= 0;
	//bestGlobalRevVal = 0;
	for(int i = id + 1; i < totalNodes; i++){
		//PutLL(i, best);
		Receive(i);
		shiftRev += GetLL(i);
		//bestGlobalRevVal = max(bestGlobalRevVal,GetLL(i));
	}

	for(int i = id - 1; i >= 0 ; i--){
		//PutLL(i, bestRev);
		Receive(i);
		shift += GetLL(i);
		//bestGlobalVal = max(bestGlobalVal,GetLL(i));
	}
}

void dump(){
	printf("%lld %lld %lld %lld %lld %lld\n", best, bestPos, bestRev, bestRevPos, shift, shiftRev);
	printf("accval ");
	//for(int i = 0; i < (int)accValues.size(); i++) printf("%lld ",accValues[i]);
	printf("\nrevval ");
	//for(int i = 0; i < (int)accValuesRev.size(); i++) printf("%lld ",accValuesRev[i]);
	printf("\n");
}

void sendMax(){
	PutLL(0, best + shift);
	PutLL(0, bestPos);
	PutLL(0, bestRev + shiftRev);
	PutLL(0, bestRevPos);
	Send(0);
}

void response(){
	Receive(0);
	long long int fromRightBound = GetLL(0);
	long long int fromLeftBound = GetLL(0);
	//printf("request %lld %lld\n",fromRightBound,fromLeftBound);
	//patrzymy z prawej
	if (gEnd <= fromRightBound + 1){//nie zahacza o nas
		PutLL(0,0);
	} else if (gBegin >= fromRightBound) {//calkowicie nas przykrywa
		//assert(accMaxValuesRev.back() > 0);
		//if ()
		//long long int ourMax = accValuesRev.front()+ shiftRev;
		//for(int i = 1; i < (int)accValuesRev.size();i++) ourMax = max (ourMax, accValuesRev[i] + shiftRev);
		long long int ourMax = shiftRev + GetTaste(gEnd - 1);
		long long int acc = GetTaste(gEnd - 1);
		for(int i = gEnd - 2; i >= gBegin; i--) {
			acc += GetTaste(i);
			ourMax = max (ourMax, acc + shiftRev);
		}
		PutLL(0,ourMax);
	} else {
		//long long int ourMax = accValuesRev.front()+ shiftRev;
		//for(int i = 1; i < gEnd - fromRightBound - 1; i++) ourMax = max (ourMax, accValuesRev[i] + shiftRev);

		long long int ourMax = shiftRev + GetTaste(gEnd - 1);
		long long int acc = GetTaste(gEnd - 1);
		for(int i = gEnd - 2; i > fromRightBound; i--) {
			acc += GetTaste(i);
			ourMax = max (ourMax, acc + shiftRev);
		}

		//PutLL(0,accMaxValuesRev[gEnd - gBegin + accMaxValuesRev.size() - 1 - fromRightBound] + shiftRev);
		PutLL(0,ourMax);
	}
	//patrzymy z lewej
	if (fromLeftBound <= gBegin){//nie zahacza o nas
		PutLL(0,0);
	} else if ( gEnd <= fromLeftBound) {//calkowicie nas przykrywa
		//PutLL(0,accMaxValues.back() + shiftRev);
		long long int ourMax = shift + GetTaste(gBegin);
		long long int acc = GetTaste(gBegin);
//		long long int ourMax = accValues.front() + shift;
		for(int i = gBegin + 1; i < gEnd; i++) {
			acc += GetTaste(i);
			ourMax = max (ourMax, acc + shift);
		}
		PutLL(0,ourMax);
	} else {
		//long long int ourMax = accValues.front()+ shift;
//		for(int i = 1; i < fromLeftBound - gBegin; i++) {
//			ourMax = max (ourMax, accValues[i] + shift);
//		}
		long long int ourMax = shift + GetTaste(gBegin);
		long long int acc = GetTaste(gBegin);
//		long long int ourMax = accValues.front() + shift;
		for(int i = gBegin + 1; i < fromLeftBound; i++) {
			acc += GetTaste(i);
			ourMax = max (ourMax, acc + shift);
		}
		PutLL(0,ourMax);
		//PutLL(0,accMaxValues[gEnd - gBegin + fromRightBound] + shiftRev);
	}


	Send(0);
}

void getMax( int total){
	long long int globalMax = best + shift;
	long long int globalMaxPos = bestPos;
	long long int globalMaxRev = bestRev + shiftRev;
	long long int globalMaxRevPos = bestRevPos;
	//long long int totalResult = globalMax + (accMaxValuesRev.size() > bestPos ? accMaxValuesRev[bestPos] : 0);
	//long long int totalResultRev = globalMaxRev + (bestRevPos >= 0 ? accMaxValues[bestRevPos] : 0);
	//totalResult = max(totalResult, totalResultRev);
	int leftBound = 0;
	int rightBound = 0;
	for(int i = 1; i < total; i++){
		Receive(i);
		long long int maxVal = GetLL(i);
		long long int maxValPos = GetLL(i);
		//long long int maxOppositeValue = GetLL(i);
		long long int maxValRev = GetLL(i);
		long long int maxValRevPos = GetLL(i);
		//long long int maxRevOppositeValue = GetLL(i);
		if (globalMax < maxVal){//tutaj musi byc ostro gdyz chcemy jak najkrotszy przedizal
			globalMax = maxVal;
			globalMaxPos = maxValPos;
			leftBound = i;
		}
		if (globalMaxRev <= maxValRev){//tutaj z kolei chcemy miec jak najkrotszy przedzial od tylu
			globalMaxRev = maxValRev;
			globalMaxRevPos = maxValRevPos;
			rightBound = i;
		}
	}

	//printf("BEST VALUES %lld [%lld]   rev %lld [%lld]  bounds %d %d\n",globalMax,globalMaxPos, globalMaxRev, globalMaxRevPos,leftBound,rightBound);

	for(int i = 0; i < total; i++){
		PutLL(i,globalMaxPos);
		PutLL(i,globalMaxRevPos);
		Send(i);
	}
	response();
	long long int bestResult = 0;
	for(int i = 0; i < total; i++){
		Receive(i);
		long long int rightBest = max(0,GetLL(i));
		long long int leftBest = max(0,GetLL(i));
		//printf("BOUNDS %d from left %lld from right %lld\n", i, leftBest, rightBest);
		bestResult = max (bestResult, globalMax + rightBest);
		bestResult = max (bestResult, globalMaxRev + leftBest);
	}
	printf("%lld\n",bestResult);
}



int main(){
	int id = MyNodeId();
	int totalNodes = NumberOfNodes();
	//printf("NODE %d %d\n",NumberOfNodes(), MyNodeId());

	long long int size = GetN();
	long long int part = size / totalNodes;
	gBegin = id * part;
	gEnd = (id == totalNodes - 1) ? size: (id + 1) * part;

	if (GetN() < NumberOfNodes()){
		if (id > 0) return 0;
		//TODO
		totalNodes = 1;
		gBegin = 0;
		gEnd = size;
		//return 0;
	}


	loadData();
	loadRevData();

	//wysylamy info o naszych maksimach
	sendData(id, totalNodes);

	//odbieramy dane o innych maksimach
	getData(id, totalNodes);


	//dump();

	if (id > 0){
		sendMax();
		response();
	} else {
		getMax(totalNodes);
	}



	/*
	if (id > 0) {
		PutInt(0, MyNodeId());
		Send(0);
	} else {
		for(int i = 1; i< NumberOfNodes(); i++) Receive(i);
		for(int i = 1; i< NumberOfNodes(); i++) total+=GetInt(i);
		printf("res %d\n", total);
		printf("%lld\n", GetN());
	}*/
	return 0;
}