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

using namespace std;

long long msum_inside(int a, int b) {
	long long sum = 0;
	long long msum = 0;
	for(int i=a; i<=b; ++i) {
		long long x = ElementAt(i);
		sum += x;
		if(sum > msum) msum = sum;
		if(sum < 0) sum = 0;
	}

	return msum;
}

long long msum_left(int a, int b) {
	long long sum = 0;
	long long msum = 0;
	for(int i=a; i<=b; ++i) {
		long long x = ElementAt(i);
		sum += x;
		if(sum > msum) msum = sum;
	}

	return msum;
}

long long msum_right(int a, int b) {
	long long sum = 0;
	long long msum = 0;
	for(int i=b; i>=a; --i) {
		long long x = ElementAt(i);
		sum += x;
		if(sum > msum) msum = sum;
	}

	return msum;
}

long long full_sum(int a, int b) {
	long long sum = 0;
	for(int i=a; i<=b; ++i)
		sum += ElementAt(i);
	return sum;
}

vector<long long> ins, vleft, vright, vfull;

long long collect() {
	for(int i=1; i<NumberOfNodes(); ++i) {
		Receive(i);
		ins.push_back(GetLL(i));
		vleft.push_back(GetLL(i));
		vright.push_back(GetLL(i));
		vfull.push_back(GetLL(i));
	}

	long long msum = *max_element(ins.begin(), ins.end());
	long long sum = vright.front();

	for(int i=1; i<NumberOfNodes(); ++i) {
		msum = max(msum, sum + vleft[i]);
		sum = max(vright[i], sum+vfull[i]);
	}

	cout << msum;
}


int main() {
	long long first = (MyNodeId() * Size()) / NumberOfNodes() + 1;
	long long last = ((MyNodeId() + 1) * Size()) / NumberOfNodes();

	long long cinside = msum_inside(first, last),
		cleft = msum_left(first, last),
		cright = msum_right(first, last),
		csum = full_sum(first, last);

	if(MyNodeId()) {
		PutLL(0, cinside);
		PutLL(0, cleft);
		PutLL(0, cright);
		PutLL(0, csum);
		Send(0);
	} else {
		ins.push_back(cinside);
		vleft.push_back(cleft);
		vright.push_back(cright);
		vfull.push_back(csum);

		collect();
	}

	return 0;
}