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


int main(){
	long long n = GetN() ;
	long long num = NumberOfNodes();
	long long part = n/num;
	long long id = MyNodeId();
	long long g = ((id+1)==num)?(n):((id+1)*part);
	long long maxI = 0;
	long long maxJ = 0;
	long long sumI = 0;
	long long sumJ = 0;
	long long indI = -1;
	long long indJ = n;
	for(long long j = g-1, i = id*part; i < g; ++i, --j){
		sumI += GetTaste(i);
		sumJ += GetTaste(j);
		if(sumI > maxI) {
			maxI = sumI;
			indI = i;
		}
		if(sumJ > maxJ) {
			maxJ = sumJ;
			indJ = j;
		}
		//printf("sumI = %lld sumJ = %lld\n", sumI, sumJ); 
	}
	if(id > 0){
		PutLL(0, sumI);
	    PutLL(0, maxI);
	    PutLL(0, indI);
	    PutLL(0, sumJ);
	    PutLL(0, maxJ);
	    PutLL(0, indJ);
    	Send(0);
	}else{
		
		long long getmaxI[num];
		long long getmaxJ[num];
		long long getsumI[num];
		long long getsumJ[num];
		long long getindI[num];
		long long getindJ[num];
		
		getmaxI[0]=maxI;
		getmaxJ[0]=maxJ;
		getsumI[0]=sumI;
		getsumJ[0]=sumJ;
		getindI[0]=indI;
		getindJ[0]=indJ;
	    for(int k = 1; k < num; ++k){
		int inst = Receive(-1);
	    	getsumI[inst] = GetLL(inst);
		    getmaxI[inst] = GetLL(inst);
		    getindI[inst] = GetLL(inst);
		    getsumJ[inst] = GetLL(inst);
		    getmaxJ[inst] = GetLL(inst);
		    getindJ[inst] = GetLL(inst);
		}
		maxI = 0;
		indI = -1;
		long long idI = (getindI[0] >= 0)?(0):(-1);
		for(long long k = 1; k < num; ++k){
			getsumI[k] += getsumI[k-1];
			getmaxI[k] += getsumI[k-1];
			if(maxI < getmaxI[k]){
				indI = getindI[k];
				maxI = getmaxI[k];
				idI = k;
			}
		}
		maxJ = getmaxJ[num-1];
		indJ = getindJ[num-1];
		long long idJ = (getindJ[num-1] >= 0)?(num-1):(-1);
		for(long long k = num-2; k >= 0; --k){
			getsumJ[k] += getsumJ[k+1];
			getmaxJ[k] += getsumJ[k+1];
			if(maxJ < getmaxJ[k]){
				indJ = getindJ[k];
				maxJ = getmaxJ[k];
				idJ = k;
			}
		}
		if(indI >= indJ) {
			if(maxI > maxJ){
				sumJ = ((idI+1) < num)?(getsumJ[idI+1]):0;
				indJ = -1;
				maxJ = 0;
				g = ((idI+1)==num)?(n):((idI+1)*part);
				for(long long j = g-1; j > indI; --j){
					sumJ += GetTaste(j);
					if(sumJ > maxJ) {
						maxJ = sumJ;
						indJ = j;
//printf("dupa\n");
					}
				}

		//printf("maxI = %lld maxJ = %lld\n", maxI, maxJ); 
		//printf("indI = %lld indJ = %lld\n", indI, indJ);
			}else{
				sumI = ((idJ-1) > 0)?(getsumI[idJ-1]):0;
				indI = 0;
				maxI = 0;
				g = ((idJ+1)==num)?(n):((idJ+1)*part);
				for(long long i = id*part; i < indJ; i++){
					sumI += GetTaste(i);
					if(sumI > maxI) {
						maxI = sumI;
						indI = i;
					}
				}
			}
		}
		long long result = maxI + maxJ;
		printf("%lld\n", result);
	}

}