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
/*	Arkadiusz Wróbel
 *
 *	Konkurs: Potyczki Algorytmiczne 2015
 *	Zadanie: Kanapka
 */

#include "kanapka.h"
#include "message.h"

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#include <cstdio>
#include <iostream>

#include <algorithm>
#include <cmath>
#include <cstring>

#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<PII> VPII;
typedef vector<VI> VVI;

#define REP(I, N) for(int I = 0; I < (N); ++I)
#define FOR(I, M, N) for(int I = (M); I <= (N); ++I)
#define FORD(I, M, N) for(int I = (M); I >= (N); --I)
//#define FOREACH(IT, CON) for(__typeof((CON).begin()) IT = (CON).begin(); IT != (CON).end(); ++IT)

#define ST first
#define ND second
#define MP make_pair
#define PB push_back
#define SIZE(CON) ((int)(CON).size())
#define ALL(CON) (CON).begin(), (CON).end()

const int INF = 1000000000;
const LL INFLL = 1000000000000000000LL;

//######################################################################

void make() {
	int hostsCount = NumberOfNodes();
	int host = MyNodeId();
	LL N = GetN();
	LL begin, end;
	
	{
		LL n = (N + hostsCount - 1) / hostsCount;
		begin = host * n;
		end = (host + 1) * n - 1;
		if (host == hostsCount - 1) {
			end = N - 1;
		}
	}
	
	LL localSum = 0;
	LL localMaxSum = 0;
	FOR(i, begin, end) {
		localMaxSum = max(localMaxSum, localSum);
		localSum += GetTaste(i);
	}
	
	/* Prefiksowe maksima sum prefiksowych, przesunięte o jeden w prawo */
	LL leftSum = 0;
	LL leftMaxSum = 0;
	if (host > 0) {
		Receive(host - 1);
		leftSum = GetLL(host - 1);
		leftMaxSum = GetLL(host - 1);
	}
	
	LL mySum = leftSum + localSum;
	LL myMaxSum = max(leftMaxSum, leftSum + localMaxSum);
	
	if (host < hostsCount - 1) {
		PutLL(host + 1, mySum);
		PutLL(host + 1, myMaxSum);
		Send(host + 1);
	}
	
	/* Sumy sufiksowe */
	LL rightSufSum = 0;
	if (host < hostsCount - 1) {
		Receive(host + 1);
		rightSufSum = GetLL(host + 1);
	}
	
	LL mySufSum = localSum + rightSufSum;
	
	if (host > 0) {
		PutLL(host - 1, mySufSum);
		Send(host - 1);
	}
	
	/* Szukanie wyniku */
	LL sum = leftSum;
	LL maxSum = leftMaxSum;
	LL sufSum = mySufSum;
	
	LL wy = 0;
	
	FOR(i, begin, end) {
		maxSum = max(maxSum, sum);
		
		wy = max(wy, maxSum + max(sufSum, 0LL));
		
		LL taste = GetTaste(i);
		sum += taste;
//		printf("%2d (%3lld): %3lld %3lld | %3lld %3lld\n", i, GetTaste(i), sufSum, sum, maxSum, wy);
		sufSum -= taste;
	}
	
	if (host < hostsCount - 1) {
		Receive(host + 1);
		wy = max(wy, GetLL(host + 1));
	}
	if (host > 0) {
		PutLL(host - 1, wy);
		Send(host - 1);
	} else {
		printf("%lld\n", wy);
	}
}

void makeStatic() {
	if (MyNodeId() != 0) {
		exit(0);
	}
	
	LL N = GetN();
	
	LL sufSum = 0;
	
	FORD(i, N-1, 0) {
		sufSum += GetTaste(i); 
	}
	
	LL sum = 0;
	LL maxSum = 0;
	
	LL wy = 0;
	REP(i, N) {
		maxSum = max(maxSum, sum);
		
		wy = max(wy, maxSum + max(sufSum, 0LL));
		
		sum += GetTaste(i);
//		printf("%2d (%lld): %3lld %3lld | %3lld %3lld\n", i, GetTaste(i), sufSum, sum, maxSum, wy);
		sufSum -= GetTaste(i);
	}
	
	printf("%lld\n", wy);
}

int main()
{
	if (NumberOfNodes() <= 2300000) {
		makeStatic();
	} else {
		make();
	}
//	make();
	return 0;
}