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

//try Kadane's alg and then chain
vector<long long> kadane(vector<long long> const & arr)
{
       long long max_=0,max_front=0,max_back=0,sum=0;
       for(auto i:arr){
    	   max_back=max(0LL,max_back);
    	   max_back+=i;
    	   max_=max(max_back,max_);
    	   sum+=i;
    	   max_front=max(max_front,sum);
       }
       max_back=max(0LL,max_back);
       vector<long long> m={max_,max_front,max_back,sum};
       return m;
}

int main() {
	int s=Size();
	const int d=500000;
	//how to choose the number of Nodes to use?
	 int N=min(NumberOfNodes(),s/d+1);
	vector<long long> v;
	vector<long long> res;
	//devide into N subarrays (N+1 if d|s - to change)
	for(int i=1+MyNodeId()*d;i<=d*(MyNodeId()+1) && i<=s ;++i){
		v.push_back(ElementAt(i));
		}
	//call kadane fo each subarray 
	res=kadane(v);
 	vector<long long> t;
	if(N==1 && !MyNodeId())cout<<res[0];
  	if (MyNodeId() > 0 && MyNodeId()<N) {
    		Receive(MyNodeId() - 1);
		for(int k=0;k<4;++k)
			t.push_back(GetLL(MyNodeId() - 1));
		res[0]=(max(max(t[0],res[0]),t[2]+res[1]));
		res[1]=(max(t[1],t[3]+res[1]));
		res[2]=(max(res[2],res[3]+t[2]));
		res[3]=(res[3]+t[3]);
		if(MyNodeId()==N-1) cout<<res[0]<<endl;
	 } 
	if (MyNodeId() < N - 1) {
    		for(int k=0;k<4;++k)
			PutLL(MyNodeId() + 1, res[k]);
		Send(MyNodeId() + 1);
  	}
 	 return 0;
}