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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#include <iostream>
#include <string>
#include <cctype>
#include "message.h"
#include "kanapka.h"

#ifdef __DEB
#include <fstream>
#include <cstdio>
#endif

#define RAM_LIMIT	2000000
#define MINIMUM_N 100000

using namespace std;

struct WYNIKI_PROCESOW
{	
	int64_t suma;
	int64_t max_prawo;
	int64_t max_lewo;
	int64_t max;
	int32_t nstart, nstop;
};

int main()
{
	int  ilosc_procesow = NumberOfNodes();
	if(ilosc_procesow==1)
	{
		//brutal na szybko
		int64_t n = GetN();
		if (n > 10000000) n = 10000000;
		int64_t *max_prawo = new int64_t[n];
		int64_t *max_lewo = new int64_t[n];
		
		int64_t i = 0;
		int64_t max = 0;
		int64_t suma = 0;
		while(i < n)
		{
			suma += GetTaste(i);
			if (suma > max) max = suma;
			max_prawo[i] = max;
			i++;
		}
		
		suma = 0;
		max = 0;
		i = n-1;
		while(i>=0)
		{
			suma += GetTaste(i);
			if (suma > max) max = suma;
			max_lewo[i] = max;
			i--;
		}
		
	/*	i = 0;
		while(i < n)
		{
			cout << max_prawo[i] << " " << max_lewo[i] << endl;
			i++;
		}*/
		
		max = 0;
		int32_t dlai = -1;
		i = 0;
		while(i < n-1)
		{
			suma = max_prawo[i] + max_lewo[i+1];
			if (suma > max) 
			{
		//		cout << suma << " " << max_prawo[i] << " " << max_lewo[i+1] << endl;
				max = suma;
				dlai = i;
			}
			i++;
		}
#ifdef __DEB
		char nazwa_pliku[20]; sprintf(nazwa_pliku, "deb_0.txt");
		std::ofstream deb (nazwa_pliku, std::ofstream::out);
		
		int64_t imax = max_prawo[dlai];
		suma = 0;
		i = 0;
		while(i <= dlai)
		{
			deb << GetTaste(i) << " ";
			suma += GetTaste(i);
			if (max_prawo[i] == imax) break;
			i++;
		}

		deb << endl << "** " << suma << endl;
		deb << "od: " << i << endl;

		suma = 0;		
		imax = max_lewo[dlai+1];
		//deb << imax << endl;
		i = n-1;
		while(i > dlai)
		{
			deb << GetTaste(i) << " ";
			suma += GetTaste(i);
			if (max_lewo[i] == imax) break;
			i--;
		}
		deb << endl << "** " << suma << endl;
		deb << "do: " << i << endl;
				
#endif		
				
		cout << max << endl;
		
		delete[] max_prawo;
		delete[] max_lewo;
	}
	else
	{
		
		int32_t n = GetN();
		int32_t nstart, nstop;
		int nasz_proces = MyNodeId();

#ifdef __DEB
		char nazwa_pliku[20]; sprintf(nazwa_pliku, "deb_%d.txt", nasz_proces);
		std::ofstream deb (nazwa_pliku, std::ofstream::out);
#endif				

		if (n < MINIMUM_N)
		{
#ifdef __DEB
			deb << "Zostawienie jednego procesu";
#endif				
			if (!nasz_proces)
			{
				ilosc_procesow = 1;
				nstart = 0;
				nstop = n-1;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			if (nasz_proces == ilosc_procesow-1)
			{
				//ostatni proces dostaje wszystko co poprzedni + jakieś wyrównanie jeśli zajdzie potrzeba
				int32_t tmp = n / ilosc_procesow;
				nstart = tmp * nasz_proces;
				nstop = n-1;
			}
			else
			{
				int32_t tmp = n / ilosc_procesow;
				nstart = tmp * nasz_proces;
				nstop = tmp * (nasz_proces+1) - 1;
			}
		}
		
		//ze wzg. na limit pamięci jeszcze ekstra dodatkowy podział zastosujemy
		
		int32_t wndil = nstop-nstart+1;

		if (wndil % RAM_LIMIT)
		{
			//niepodzielne
			wndil /= RAM_LIMIT;
			wndil++;
		}
		else
		{
			//podzielne
			wndil /= RAM_LIMIT;
		}
#ifdef __DEB
		deb << "wndil: "<<wndil<<endl;
		deb << "proces: " << nasz_proces << " " << nstart << " " << nstop << endl;
#endif
		
		WYNIKI_PROCESOW *wnd = new WYNIKI_PROCESOW[wndil];

		int64_t *p_max_prawo = new int64_t[RAM_LIMIT+20];
		int64_t *p_max_lewo = new int64_t[RAM_LIMIT+20];

		int32_t porcja_start = nstart;
		int wndnr = 0;
		
		while(porcja_start <= nstop)
		{
			int64_t suma_prawo = 0;
			int64_t max_prawo = 0;
			int64_t suma_lewo = 0;
			int64_t max_lewo = 0;

			int32_t i = porcja_start;
			int32_t j = i + RAM_LIMIT -1;
			if (j > nstop) j = nstop;
			int32_t roznica = j-i;
			int32_t k = 0;
			int32_t jstop = j;
#ifdef __DEB
		deb << "i: "<<i<<" j: "<<j<<endl;
#endif
			while(i <= jstop)
			{
				suma_prawo += GetTaste(i);
				if (suma_prawo > max_prawo)
				{
					max_prawo = suma_prawo;
				}
				p_max_prawo[k] = max_prawo;
				suma_lewo += GetTaste(j);
				if (suma_lewo > max_lewo)
				{
					max_lewo = suma_lewo;
				}
				p_max_lewo[roznica-k] = max_lewo;
				k++;
				i++;
				j--;
			}
			
			int64_t max = 0;
			int64_t suma = 0;
			if (roznica)
			{
				k = 0;
				while(k < roznica)
				{
					suma = p_max_prawo[k] + p_max_lewo[k+1];
					if (suma > max) max = suma;
					k++;
				}
			}
			else
			{
				max = suma_prawo;
			}
		
			wnd[wndnr].nstart = porcja_start;
			wnd[wndnr].nstop = jstop;
			wnd[wndnr].suma = suma_prawo;
			wnd[wndnr].max_prawo = max_prawo;
			wnd[wndnr].max_lewo = max_lewo;
			wnd[wndnr].max = max;
			
#ifdef __DEB
		deb << "*** [" << suma_prawo << "] " << max_prawo << " "  << " " << max_lewo << " max:" << max << endl;
		{int i = 0;
		while(i < roznica+1)
		{
			deb << p_max_prawo[i] << " " << p_max_lewo[i] << endl;
			i++;
		}}		
#endif

			wndnr++;
			porcja_start += RAM_LIMIT;
		}

#ifdef __DEB
		{int i = 0;
		while(i < wndil)
		{
			deb << "n: " << wnd[i].nstart << " " << wnd[i].nstop << endl;
			deb << "suma: " << wnd[i].suma << endl;
			deb << "prawo: " << wnd[i].max_prawo << endl;
			deb << "lewo: " << wnd[i].max_lewo << endl;
			deb << "max: " << wnd[i].max << endl << endl;
			i++;
		}}		
#endif
		
			//znamy już wyniki procesów, więc lecimy z obliczeniami
			int64_t max = 0;
			int64_t suma_prawo = 0;
			int64_t suma_lewo = 0;
			int32_t i = 0;
			while(i < wndil-1)
			{
				suma_prawo += wnd[i].max_prawo;
				
#ifdef __DEB
				deb << "suma_prawo+max_prawo[" << i << "]: " << suma_prawo << endl;
#endif
				
				suma_lewo = 0;
				int64_t suma;
				int32_t j = wndil-1;
				while(j>i)
				{
					suma_lewo += wnd[j].max_lewo;
#ifdef __DEB
				deb << "		suma_lewo+max_lewo[" << j << "]: " << suma_lewo << endl;
#endif
					suma = suma_lewo + suma_prawo;
					if (suma > max) 
					{
						max = suma;
#ifdef __DEB
				deb << " ** nowe max: " << suma << endl;
#endif
					}

					suma_lewo -= wnd[j].max_lewo;
					suma_lewo += wnd[j].suma;
					j--;
				}

				suma_prawo -= wnd[i].max_prawo;
				suma_prawo += wnd[i].suma;
				
				i++;
			}
//max = 0;
			//do tej pory sprawdzialiśmy przypadki pomiędzy zestawami, ale nie przypadki, kiedy wynik mógł być w jednym zestawie
			//a teraz trzeba przetestować co by było, gdyby wynik znajdował się dokładnie w jednym z zestawów
			i = 0;
			while(i < wndil)
			{
				int64_t suma = 0;
				int32_t j = 0;
				while(j < wndil)
				{
					if (j != i)
					{
						suma += wnd[j].suma;
					}
					else
					{
						suma += wnd[j].max;
					}
					j++;
				}
				if (suma > max)
				{
					max = suma;
					#ifdef __DEB
									deb << " ** nowe max: " << suma << " dla: " << i << endl;
					#endif
				}
				i++;
			}						
		
			WYNIKI_PROCESOW dosend;
			dosend.nstart = nstart;
			dosend.nstop = nstop;
			dosend.max = max;
			dosend.suma = 0;
			i = 0;
			while(i < wndil)
			{
				dosend.suma += wnd[i].suma;
				i++;
			}
			
			i=0;
			max = 0;
			suma_prawo = 0;
			while(i <= wndil-1)
			{
				suma_prawo += wnd[i].max_prawo;

				if (suma_prawo > max) max = suma_prawo;
				
				suma_prawo -= wnd[i].max_prawo;
				suma_prawo += wnd[i].suma;
				i++;
			}
			dosend.max_prawo = max;

			suma_lewo = 0;
			max = 0;
			int32_t j = wndil-1;
			while(j>=0)
			{
				suma_lewo += wnd[j].max_lewo;

				if (suma_lewo > max) max = suma_lewo;
				
				suma_lewo -= wnd[j].max_lewo;
				suma_lewo += wnd[j].suma;
				j--;
			}
			
			dosend.max_lewo = max;
			
#ifdef __DEB
			deb << "dosend" << endl << "n: " << dosend.nstart << " " << dosend.nstop << endl;
			deb << "suma: " << dosend.suma << endl;
			deb << "prawo: " << dosend.max_prawo << endl;
			deb << "lewo: " << dosend.max_lewo << endl;
			deb << "max: " << dosend.max << endl << endl;
#endif			
			
		if (!nasz_proces)
		{
			WYNIKI_PROCESOW wp[ilosc_procesow];
			wp[0] = dosend;
			
			i = 1;
			while(i < ilosc_procesow)
			{
				Receive(i);
				wp[i].suma = GetLL(i);
				wp[i].max_prawo = GetLL(i);
				wp[i].max_lewo = GetLL(i);
				wp[i].max = GetLL(i);
				wp[i].nstart = GetInt(i);
				wp[i].nstop = GetInt(i);
				i++;
			}
			
#ifdef __DEB
			i = 0;
			while(i < ilosc_procesow)
			{
				deb << "Proces: " << i << endl;
				deb << "n " << wp[i].nstart << " " << wp[i].nstop << endl;
				deb << "Suma: " << wp[i].suma<< endl;
				deb << "Prawo: " << wp[i].max_prawo<< endl;
				deb << "Lewo: " << wp[i].max_lewo<< endl;
				deb << "MAX: " << wp[i].max<< endl;
				i++;
			}
#endif

			int64_t max = 0;
			int64_t suma_prawo = 0;
			int64_t suma_lewo = 0;
			int32_t i = 0;
			while(i < ilosc_procesow-1)
			{
				suma_prawo += wp[i].max_prawo;
				
#ifdef __DEB
				deb << "suma_prawo+max_prawo[" << i << "]: " << suma_prawo << endl;
#endif
				
				suma_lewo = 0;
				int64_t suma;
				int32_t j = ilosc_procesow-1;
				while(j>i)
				{
					suma_lewo += wp[j].max_lewo;
#ifdef __DEB
				deb << "		suma_lewo+max_lewo[" << j << "]: " << suma_lewo << endl;
#endif
					suma = suma_lewo + suma_prawo;
					if (suma > max) 
					{
						max = suma;
#ifdef __DEB
				deb << " ** nowe max: " << suma << endl;
#endif
					}

					suma_lewo -= wp[j].max_lewo;
					suma_lewo += wp[j].suma;
					j--;
				}
				if (i == 0)
				{
					suma_lewo += wp[0].max_lewo;
					if (suma_lewo > max) 
					{
						max = suma_lewo;
		#ifdef __DEB
				deb << " ** nowe max: " << max << endl;
		#endif
					}
				}

				suma_prawo -= wp[i].max_prawo;
				suma_prawo += wp[i].suma;
				
				i++;
			}
			
			//jeszcze ekstra tylko dla sumy w prawo i tylko dla sumy w lewo
			suma_prawo += wp[i].max_prawo;
			if (suma_prawo > max) 
			{
				max = suma_prawo;
#ifdef __DEB
		deb << " ** nowe max: " << max << endl;
#endif
			}
			
			
//max = 0;
			//do tej pory sprawdzialiśmy przypadki pomiędzy zestawami, ale nie przypadki, kiedy wynik mógł być w jednym zestawie
			//a teraz trzeba przetestować co by było, gdyby wynik znajdował się dokładnie w jednym z zestawów
			i = 0;
			while(i < ilosc_procesow)
			{
				int64_t suma = 0;
				int32_t j = 0;
				while(j < ilosc_procesow)
				{
					if (j != i)
					{
						suma += wp[j].suma;
					}
					else
					{
						suma += wp[j].max;
					}
					j++;
				}
				if (suma > max)
				{
					max = suma;
					#ifdef __DEB
									deb << " ** nowe max: " << suma << " dla: " << i << endl;
					#endif
				}
				i++;
			}				

			cout << max << endl;
		}
		else
		{
			PutLL(0, dosend.suma);
			PutLL(0, dosend.max_prawo);
			PutLL(0, dosend.max_lewo);
			PutLL(0, dosend.max);
			PutInt(0, nstart);
			PutInt(0, nstop);
			Send(0);
		}
		delete[] wnd;
		delete[] p_max_prawo;
		delete[] p_max_lewo;
	}
	return 0;
}