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
#include <algorithm>
#include <cstdio>

#include "message.h"
#include "maklib.h"

#define MAX_P 100
//#define MAX_N_DIV_P 10000001

typedef long long LL;

int id,nodes_count,elements_total,elements_count;
//LL E[MAX_N_DIV_P];
LL element,sum,lmax,anymax,tmp_anymax,rmax;
LL SUM[MAX_P],LMAX[MAX_P],RMAX[MAX_P];
LL result; 

void Maxes();
 
int main()
{
	id = MyNodeId();
	elements_total = Size();
	nodes_count = std::min(NumberOfNodes(),elements_total);
	elements_count = elements_total / nodes_count;
	if(elements_count * nodes_count < elements_total) ++elements_count;
	
	Maxes();
	
	if(id)
	{
		if(id >= nodes_count) return 0;
		PutLL(0,anymax);
		PutLL(0,sum);
		PutLL(0,lmax);
		PutLL(0,rmax);
		Send(0);
	}
	else // id = 0
	{
		result = anymax;
		//SUM[0] = sum;
		//LMAX[0] = lmax;
		//RMAX[0] = rmax;
	
		for(int i = nodes_count; --i > 0;)
		{
			int src = Receive(-1);
			result = std::max(result,GetLL(src));
			SUM[src] = GetLL(src);
			LMAX[src] = GetLL(src);
			RMAX[src] = GetLL(src);
		}
		
		LL lpart_max = rmax;
		for(int i = 1; i < nodes_count; ++i)
		{
			result = std::max(result,lpart_max + LMAX[i]);
			lpart_max = std::max(RMAX[i],lpart_max + SUM[i]);
		}
		
		printf("%lld",result);
	}
	
	return 0;
}

void Maxes()
{
	int i = 0;
	int k = id*elements_count,end = std::min((id+1)*elements_count,elements_total);
	for(; k < end; ++i)
	{
		//E[i] = element = ElementAt(++k);
		element = ElementAt(++k);
		sum += element;
		lmax = std::max(lmax,sum);
		tmp_anymax = std::max(0LL,tmp_anymax + element);
		anymax = std::max(anymax,tmp_anymax);
	}
	sum = 0LL;
	while(i--)
	{
		//sum += E[i];
		sum += ElementAt(k--);
		rmax = std::max(rmax,sum);
	}
}