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
//============================================================================
// Author      : Grzegorz Gajoch
//============================================================================

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <list>

#include "maklib.h"
//#include "../local/message.h"
#include "message.h"

using namespace std;

#define FOR(i,a,b) for(int i = a; i <= b; ++i)
#define FORD(i,a,b) for(int i = a; i >= b; --i)
#define REP(x, n) for(int x=0; x < (n); ++x)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define SIZE(n) (int)n.size()
#define ALL(c) c.begin(),c.end()
#define PB(n) push_back(n)
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<bool> VB;
#define MP make_pair
#define ST first
#define ND second
const int INF = 1000000001;

vector<long long> B, E, M, S, T, K;
int main()
{
	int nodes = NumberOfNodes(),
		nodeNr = MyNodeId(),
		N = Size(),
		l;
	if (N % nodes == 0)
		l = (N / nodes);
	else
		l = ((int)(N/nodes)) + 1;

	int begin = min(nodeNr*l,N),
		end = min(N,begin + l);
	LL b = 0, b_max = 0,
		e = 0, e_max = 0,
		m = 0, m_max = 0,
		s = 0;
	for (int x = begin; x < end; ++x)
	{
		int now = ElementAt(x+1);
		s += now;//calculate sum

		b += now; //begin
		b_max = max(b_max, b);

		m += now; //middle
		m_max = max(m_max, m);
		if (m < 0)
			m = 0;
	}
	for (int x = end - 1; x >= begin; --x)
	{		
		int now = ElementAt(x+1);
		e += now; //end
		e_max = max(e_max, e);
	}
	int last = nodes - 1;
	PutLL(last, b_max);
	PutLL(last, e_max);
	PutLL(last, m_max);
	PutLL(last, s);
	Send(last);
	if (nodeNr != last)
		return 0;

	FOR(i,0,last)
	{
		Receive(i);
		B.push_back(GetLL(i));
		E.push_back(GetLL(i));
		M.push_back(GetLL(i));
		S.push_back(GetLL(i));
		//printf("b=%d, e=%d, m=%d, s=%d\n", *B.rbegin(), *E.rbegin(), *M.rbegin(), *S.rbegin());
	}
	LL maksi = 0;
	LL last_elem = B[nodes - 1];
	for (int i = nodes - 2; i >= 0; --i)
	{
		LL t = max(B[i], S[i] + last_elem);
		//printf("%lld %lld %lld %lld\n", last_elem, S[i], B[i], t);
		LL k = t;
		if (i > 0) k += E[i - 1];
		maksi = max(maksi, k);
		last_elem = t;
	}
	for (auto x : M)
	{
		maksi = max(maksi, x);
	}
	cout << maksi << endl;
	//printf("(%d %d) -> b=%d, e=%d, m=%d, s=%d\n", begin,end,b_max,e_max,m_max,s);
}