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
#include "kanapka.h"
#include "message.h"
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
	int n = GetN();
	int p = NumberOfNodes();
	int k = MyNodeId();
	int t;
	long long res = 0, partialRes = 0, maxF = 0, maxB = 0, sumF = 0, sumB = 0, sumPrev = 0, maxPrev = 0, sumNext = 0, maxNext = 0, prefSum;
	int block = (n / p) + (int)(n % p != 0);

	if (k * block < n) {
		// partial sums and maxes forward
		int myBlockSize = min(n, (k + 1)*block) - k*block;
		vector<int> tastes;
		tastes.resize(myBlockSize);
		vector<long long> prefS;
		prefS.resize(myBlockSize);
		for (int i = k*block; i < min(n, (k + 1)*block); i++) {
			t = GetTaste(i);
			tastes[i - k*block] = t;
			prefS[i - k*block] = t + (i == k*block ? 0 : prefS[i - k*block - 1]);
		}
		sumF = prefS.back();

		// get previous sum and max - forward
		if (k > 0) {
			Receive(k - 1);
			sumPrev = GetLL(k - 1);
			maxPrev = GetLL(k - 1);
		}
		maxF = max(maxF, maxPrev);
		for (int i = 0; i < myBlockSize; i++) maxF = max(maxF, sumPrev + prefS[i]);
		if ((k + 1)*block < n) {
			PutLL(k + 1, sumF + sumPrev);
			PutLL(k + 1, maxF);
			Send(k + 1);
		}

		// back partial sums
		for (int i = myBlockSize - 1; i >= 0; i--) {
			int j = myBlockSize - 1 - i;
			prefS[j] = tastes[i] + (j == 0 ? 0 : prefS[j - 1]);
		}
		sumB = prefS.back();

		// get next sum and max - back
		if ((k + 1)*block < n) {
			Receive(k + 1);
			sumNext = GetLL(k + 1);
			maxNext = GetLL(k + 1);
		}
		maxB = max(maxB, maxNext);
		for (int i = 0; i < myBlockSize; i++) maxB = max(maxB, sumNext + prefS[i]);
		if (k > 0) {
			PutLL(k - 1, sumB + sumNext);
			PutLL(k - 1, maxB);
			Send(k - 1);
		}

		// calculate partial results

		// 'i' is a left end -> swap partial back sums
		for (int i = 0; i < (myBlockSize - 1) / 2; i++)
			swap(prefS[i], prefS[myBlockSize - 1 - i]);

		for (int i = myBlockSize - 2; i >= 0; i--)
			prefS[i] = max(prefS[i], prefS[i + 1]);

		prefSum = sumPrev;
		for (int i = 0; i < myBlockSize; i++) {
			prefSum += tastes[i];
			long long tail = sumNext + (i == myBlockSize - 1 ? 0 : prefS[i + 1]);
			long long current = prefSum + max(maxNext, tail);
			partialRes = max(partialRes, current);
		}

		// 'i' is a right end
		for (int i = 0; i < myBlockSize; i++)
			prefS[i] = tastes[i] + (i == 0 ? 0 : prefS[i - 1]);

		for (int i = 1; i < myBlockSize; i++)
			prefS[i] = max(prefS[i - 1], prefS[i]);

		prefSum = sumNext;
		for (int i = myBlockSize - 1; i >= 0; i--) {
			prefSum += tastes[i];
			long long tail = sumPrev + (i == 0 ? 0 : prefS[i - 1]);
			long long current = prefSum + max(maxPrev, tail);
			partialRes = max(partialRes, current);
		}
	}

	// gather results
	if (k == 0) {
		res = max(res, partialRes);
		for (int i = 1; i < p; i++) {
			Receive(i);
			partialRes = GetLL(i);
			res = max(res, partialRes);
		}
		cout << res << endl;
	}
	else {
		PutLL(0, partialRes);
		Send(0);
	}

	return 0;
}