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
#include <iostream>
#include <cmath>
#include <vector>
#include "message.h"
#include "maklib.h"
using namespace std;

typedef long long int LL;

struct ProcInfo {
	LL firstPart;
	LL middlePart;
	LL endPart;
	LL overallSum;

	ProcInfo(LL first, LL middle, LL end, LL sum) {
		firstPart = first;
		middlePart = middle;
		endPart = end;
		overallSum = sum;
	}

	ProcInfo(): ProcInfo(0) {};

	ProcInfo(int start) {
		firstPart = 0;
		middlePart = 0;
		endPart = 0;
		overallSum = 0;
	};

	void sendTo(int procNr) {
		PutLL(procNr, firstPart);
		PutLL(procNr, middlePart);
		PutLL(procNr, endPart);
		PutLL(procNr, overallSum);
	}

	static ProcInfo receiveFrom(int procNr) {
		ProcInfo ret;
		ret.firstPart = GetLL(procNr);
		ret.middlePart = GetLL(procNr);
		ret.endPart = GetLL(procNr);
		ret.overallSum = GetLL(procNr);
		return ret;
	}

	void update(LL currentVal, int nextIndex) {
		overallSum += currentVal;
		if(overallSum >= firstPart) {
			firstPart = overallSum;
		}
		endPart += currentVal;
		updateMiddlePart();
		if(endPart < 0)
			endPart = 0;
	}

	void updateMiddlePart() {
		if(endPart > middlePart) {
			middlePart = endPart;
		}
	}

	void print() {
		cout << "first: "
			<< firstPart
			<< ", best: "
			<< middlePart
			<< ", end: "
			<< endPart
			<< ", whole: "
			<< overallSum
			<< endl;
	}
};

class Process {
public:
	Process(int coordinator, int my): coordinatorNr(coordinator), myNr(my) {
		allProcs = NumberOfNodes();
		allElems = Size();
		myFirst = (allElems / allProcs) * myNr + min(allElems % allProcs, myNr) + 1;
		myAfterLast = (allElems / allProcs) * (myNr + 1) + min(allElems % allProcs, myNr+1) + 1;
		myProcInfo = ProcInfo(myFirst);
	}

	Process() {}

	virtual void exec() {
		for(int i = myFirst; i < myAfterLast; ++i) {
			int currentVal = ElementAt(i);
			int nextIndex = i+1;
			myProcInfo.update(currentVal, nextIndex);
		}
		myProcInfo.sendTo(coordinatorNr);
		Send(coordinatorNr);
	}

protected:
	ProcInfo myProcInfo;
	int myFirst;
	int myAfterLast;
	int allElems;
	int coordinatorNr;
	int myNr;
	int allProcs;
};

class Coordinator : public Process {
public:
	Coordinator(int coordinator): Process(coordinator, coordinator) {
		procCount = NumberOfNodes();
	};

	void exec() {
		prepare();
		Process::exec();
		collectResults();
	}

	void prepare() {

	}

	void collectResults() {
		vector<ProcInfo> results(allProcs);
		for(int i = 0; i < allProcs; ++i) {
			Receive(i);
			results[i] = ProcInfo::receiveFrom(i);
		}
		LL bestSum = 0;
		for(int i = 0; i < allProcs; ++i) {
			if(results[i].middlePart > bestSum) {
				bestSum = results[i].middlePart;
			}
		}
		LL currentSum = 0;
		for(int i = 0; i < allProcs; ++i) {
			LL currentLeftVal = results[i].firstPart;
			LL currentRightVal = results[i].endPart;
			LL currentThroughVal = results[i].overallSum;
			if(currentSum + currentLeftVal > bestSum)
				bestSum = currentSum + currentLeftVal;
			if(currentSum + currentThroughVal > currentRightVal) 
				currentSum += currentThroughVal;
			else
				currentSum = currentRightVal;
		}

		cout << bestSum << endl;
	}

private:
	int procCount;
};


int main() {
	int procNr = MyNodeId();
	int coordinator = 0;
	if(procNr == coordinator)
		Coordinator(procNr).exec();
	else
		Process(coordinator, procNr).exec();
	return 0;
}