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
/*
 * dawid.sieradzki@gmail.com
 */

#include "message.h"
#include "maklib.h"

#include <vector>
#include <cassert>
#include <cstdio>

using namespace std;


int len;
int node;
int nodeNum;
long long sub[5];
vector<long long> dist;


long long max_sub(int baseIdx, int (*getE)(int)) {
	long long e;

	int beginIdx = 0;
	long long currentSum = 0;
	long long bestSum = 0;

	int bestBeginIdx = len, bestEndIdx = len;

	long long totalSum = 0;
	for (int i = 0; i < len; i++) {
		e = getE(baseIdx + i);
		totalSum += e;
		if (currentSum >= 0 && currentSum + e >= 0) {
			currentSum += e;
		} else {
			currentSum = e;
			beginIdx = i;
		}

		if (currentSum >= bestSum) {
			bestBeginIdx = beginIdx;
			bestEndIdx = i + 1;

			bestSum = currentSum;
		}

		//printf("%2i [%3lli] - current = %lli, best = %lli, begin = %i\n", i, e, currentSum, bestSum, beginIdx);
	}

	sub[2] = bestSum;
	//printf("bBegin, bEnd = %i, %i\n", bestBeginIdx, bestEndIdx);

	{
		currentSum = 0;
		bestSum = 0;
		int endIdx = 0;
		for (int i = 0; i < bestBeginIdx; i++) {
			e = getE(baseIdx + i);
			currentSum += e;

			if (currentSum >= bestSum) {
				endIdx = i + 1;

				bestSum = currentSum;
			}
		}

		currentSum = 0;
		for (int i = endIdx; i < bestBeginIdx; i++) {
			e = getE(baseIdx + i);
			currentSum += e;
		}

		//printf("endIdx = %i\n", endIdx);

		sub[0] = bestSum;
		sub[1] = currentSum;
	}

	{
		currentSum = 0;
		bestSum = 0;
		int startIdx = len;
		for (int i = len - 1; i >= bestEndIdx; i--) {
			e = getE(baseIdx + i);
			currentSum += e;

			if (currentSum >= bestSum) {
				startIdx = i;

				bestSum = currentSum;
			}
		}

		currentSum = 0;
		for (int i = startIdx - 1; i >= bestEndIdx; i--) {
			e = getE(baseIdx + i);
			currentSum += e;
		}

		//printf("startIdx = %i\n", startIdx);

		sub[3] = currentSum;
		sub[4] = bestSum;
	}

	//for (int c = 0; c < 5; c++) {
		//printf("%6lli", sub[c]);
	//}
	//printf("\n");

	assert(sub[0] >= 0);
	assert(sub[1] <= 0);
	assert(sub[2] >= 0);
	assert(sub[3] <= 0);
	assert(sub[4] >= 0);
	assert(sub[0] + sub[1] + sub[2] + sub[3] + sub[4] == totalSum);

	return max(sub[0], max(sub[2], sub[4]));
}

int getDistE(int idx) {
	return dist[idx];
}


int main(int argc, char *argv[]) {
	node = MyNodeId();
	nodeNum = NumberOfNodes();

	int mod = Size() % nodeNum;
	len = Size() / nodeNum + (node == 0) * mod;
	int baseIdx = 1 + len * node + (node > 0) * mod;

	max_sub(baseIdx, ElementAt);

	for (int c = 0; c < 5; c++) {
		PutLL(0, sub[c]);
	}
	Send(0);

	if (node == 0) {
		dist.resize(len = nodeNum * 5);
		for (int n = 0; n < nodeNum; n++) {
			Receive(n);
			//printf("%3i: ", n);
			for (int c = 0; c < 5; c++) {
				dist[5 * n + c] = GetLL(n);
				//printf("%6lli", dist[5 * n + c]);
			}
			//printf("\n");
		}

		long long r = max_sub(0, getDistE);
		printf("%lli\n", r);
	}

	return 0;
}