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

typedef long long int ll;

class X
{
 public:
 virtual ll operator[](int i){return ElementAt(i+1);}
};
X x;

ll tab[1000];

class Y : public X { public: ll operator[](int i){return tab[i];}};
Y y;

void calc(X &x,int start,int end,ll &m,ll &l,ll &r,ll &s)
{
 m=0;l=0;r=0;s=0;
 for(int i=start;i<end;i++)
 {
  s += x[i];
  l = std::max(l,s);
  r = std::max(r,0LL)+x[i];
  m = std::max(m,r);
 }
 s -= l + r;
}

int main()
{
 ll N = Size();
 int M = NumberOfNodes();
 int I = MyNodeId();

 ll start = N * I / M;
 ll end   = N * (I+1) / M;

 ll m,l,r,s;

 calc(x,start,end,m,l,r,s);

 if(I != 0)
 {
  PutLL(0,m);PutLL(0,l);PutLL(0,s);PutLL(0,r);Send(0);
 }
 else
 {
  tab[0] = l; tab[1] = s; tab[2] = r;
  for(int i=1; i < M; i++)
  {
   int j = Receive(-1);
   m = std::max(m,GetLL(j));
   tab[3*j  ] = GetLL(j);
   tab[3*j+1] = GetLL(j);
   tab[3*j+2] = GetLL(j);
  }
  ll m2;
  calc(y,0,3*M,m2,l,r,s);
  m = std::max(m,m2);
  std::cout << m << std::endl;
 }
 return 0;
}