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

#include <algorithm>
#include <iostream>
#include <cstdio>

using namespace std;

const int PMAX = 100;

typedef long long ll;

int n, nodes, id, x;
ll start, stop, suma, best, bestNow, bestLeft, bestRight;
ll suma0, best0, bestLeft0, bestRight0;

int main() {
  n = Size();
  nodes = NumberOfNodes();
  id = MyNodeId();
  
  start = ((ll) id * n) / nodes + 1;
  stop = ((ll) (id + 1) * n) / nodes + 1;
  
  for(int i = start; i < stop; ++i) {
  	x = ElementAt(i);
  	suma += x;
  	
  	bestNow += x;
  	bestNow = max(bestNow, 0LL);
  	best = max(best, bestNow);
  	
  	bestLeft = max(bestLeft, suma);
  }
  bestRight = bestNow;
  
  if(id > 0) {
  	PutLL(0, suma);
  	PutLL(0, best);
  	PutLL(0, bestLeft);
  	PutLL(0, bestRight);
  	Send(0);
  }
  else {
  	for(int j = 1; j < nodes; ++j) {
  		Receive(j);
  		suma0 = GetLL(j);
  		best0 = GetLL(j);
  		bestLeft0 = GetLL(j);
  		bestRight0 = GetLL(j);
  		
  		best = max(best, best0);
  		best = max(best, bestRight + bestLeft0);
  		bestRight += suma0;
  		bestRight = max(bestRight, bestRight0);
  	}
  	printf("%lld", best);
  }
  
  return 0;
}