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
#include "kanapka.h"
#include "message.h"
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
	int n = GetN();
	int nodes = NumberOfNodes();
	int my = MyNodeId();
	
	int size = (n+nodes-2)/(nodes-1);
	
	if(n <= 100000) {
		
		if(my == 0) {
			long long int wynik = 0;
			
			int start = 0;
			int finish = n-1;
			long long int S = 0, L = 0, R = 0, B = 0;
			long long int left[nodes+5], right[nodes+5];
			
			left[0] = 0;
			right[finish-start+2] = 0;
			for(int i = start; i <= finish; i++) {
				long long int x = GetTaste(i);
				S += x;
				left[i-start+1] = max(left[i-start], S); 
				L = max(L, left[i-start+1]);
			}
			
			long long int X = 0;
			for(int i = finish; i >= start; i--) {
				long long int x = GetTaste(i);
				X += x;
				right[i-start+1] = max(right[i-start+2], X);
				R = max(R, right[i-start+1]); 
			}
			
			for(int i = start; i <= finish; i++) {
				B = max(B, left[i-start]+right[i-start+1]);
			}
			wynik = max(wynik, S);
			wynik = max(wynik, L);
			wynik = max(wynik, R);
			wynik = max(wynik, B);
			
			cout << wynik << endl;
		}
		
	} else {
		
		if(my == 0) {
			long long int wynik = 0;
			long long int info[nodes][5];
			long long int left[nodes+5], right[nodes+5];
			long long int S = 0;
			
			for(int i = 1; i <= nodes-1; i++) {
				Receive(i); 
				info[i][1] = GetLL(i);
				info[i][2] = GetLL(i);
				info[i][3] = GetLL(i);
				info[i][4] = GetLL(i);
				S = S+info[i][1];
			}
			wynik = max(wynik, S);
			
			for(int i = 1; i <= nodes-1; i++) {
				S = S - info[i][1] + info[i][4];
				wynik = max(wynik, S);
				S = S + info[i][1] - info[i][4];
			}
			left[0] = 0;
			right[nodes] = 0;
			
			for(int i = 1; i <= nodes-1; i++) {
				left[i] = left[i-1] + max(info[i][1], info[i][2]);
				wynik = max(wynik, left[i]);
			}
			
			for(int i = nodes-1; i >= 1; i--) {
				right[i] = right[i+1] + max(info[i][1], info[i][3]);
				wynik = max(wynik, right[i]);
			}
			
			for(int i = 1; i <= nodes-1; i++) {
				wynik = max(wynik, left[i-1]+right[i]);
			}
			
			cout << wynik << endl;
		}
		else {
			int start = (my-1)*size;
			int finish = min(my*size-1, n-1);
			long long int S = 0, L = 0, R = 0, B = 0;
			long long int left[size+5], right[size+5];
			
			left[0] = 0;
			right[finish-start+2] = 0;
			for(int i = start; i <= finish; i++) {
				long long int x = GetTaste(i);
				S += x;
				left[i-start+1] = max(left[i-start], S); 
				L = max(L, left[i-start+1]);
			}
			
			long long int X = 0;
			for(int i = finish; i >= start; i--) {
				long long int x = GetTaste(i);
				X += x;
				right[i-start+1] = max(right[i-start+2], X);
				R = max(R, right[i-start+1]); 
			}
			
			for(int i = start; i <= finish; i++) {
				B = max(B, left[i-start]+right[i-start+1]);
			}
			
			PutLL(0,S);
			PutLL(0,L);
			PutLL(0,R);
			PutLL(0,B);
			Send(0);
		}	
	}
	
	return 0;
}