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

struct LocalMax {
	long long index, value;
};

int main() {
	int instances = NumberOfNodes();
	int me = MyNodeId();
	long long N = GetN();
	long long dN = N/instances;
	long long begInd = me*dN;
	long long maxCandidate = 0;
	long long tmpMaxCandidate = 0LL;
	long long sumOfPart = 0LL;
	struct LocalMax otherMaxFromBeg, otherMaxFromEnd;
	struct LocalMax maxFromBeg = {0ll, 0ll}, maxFromEnd = {0ll, 0ll};

	if(instances -1 == me) {
		dN = N-begInd;
	}
	for(long long ind = begInd; ind < begInd+dN; ++ind) {
		long long value = GetTaste(ind);
		sumOfPart += value;
		if( maxFromBeg.value < sumOfPart ) {
			maxFromBeg.value = sumOfPart;
			maxFromBeg.index = ind;
		} 
		maxFromEnd.value += value;
		if( maxFromEnd.value < value ) {
			maxFromEnd.value = value;
			maxFromEnd.index = ind;
		} 
	}
	// send results to other instances
	for(int target = 0; target < instances; ++target) {
		if(me == target) {
			continue;
		}
		PutLL(target, sumOfPart);
		Send(target);	
	}
	for(int source = 0; source < instances; ++source) {
		if(me == source) {
			continue;
		}
		Receive(source);
		sumOfPart = GetLL(source);
		
		if(source < me) {
			maxFromBeg.value += sumOfPart;
		} else {
			maxFromEnd.value += sumOfPart;
		}
	}
	// send results to other instances
	for(int target = 0; target < instances; ++target) {
		if(me == target) {
			continue;
		}
		PutLL(target, maxFromBeg.value);
		PutLL(target, maxFromBeg.index);
		PutLL(target, maxFromEnd.value);
		PutLL(target, maxFromEnd.index);
		Send(target);	
	}

	if(maxFromBeg.value < 0) {
		maxFromBeg.value = 0;
		maxFromBeg.index = -1;
	}
	if(maxFromEnd.value < 0) {
		maxFromEnd.value = 0;
		maxFromEnd.index = N;
	}

	if(maxFromBeg.index < maxFromEnd.index)	{
		maxCandidate = maxFromBeg.value + maxFromEnd.value;
	} else if(maxFromBeg.value > maxFromEnd.value) {
		maxCandidate = maxFromBeg.value;
	} else {
		maxCandidate = maxFromEnd.value;
	}

	for(int source = 0; source < instances; ++source) {
		if(me == source) {
			continue;
		}
		Receive(source);
		otherMaxFromBeg.value = GetLL(source);
		otherMaxFromBeg.index = GetLL(source);
		otherMaxFromEnd.value = GetLL(source);
		otherMaxFromEnd.index = GetLL(source);

		//analyze candidates...
		if(otherMaxFromBeg.value > maxFromBeg.value && source < me) {
			tmpMaxCandidate = otherMaxFromBeg.value + maxFromEnd.value;
			if(tmpMaxCandidate > maxCandidate) {
				maxCandidate = tmpMaxCandidate;
			}
		}

		if(otherMaxFromEnd.value > maxFromEnd.value && source > me) {
			tmpMaxCandidate = otherMaxFromEnd.value + maxFromBeg.value;
			if(tmpMaxCandidate > maxCandidate) {
				maxCandidate = tmpMaxCandidate;
			}
		}
	}

	if(me != 0) {
		PutLL(0, maxCandidate);
		Send(0);
	} else {
		for(int source = 0; source < instances; ++source) {
			if(me == source) {
				continue;
			}
			Receive(source);
			tmpMaxCandidate = GetLL(source);
			if(tmpMaxCandidate > maxCandidate) {
				maxCandidate = tmpMaxCandidate;
			}
		}
		std::cout << maxCandidate;
	}
	return 0;
}