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

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n = Size();
  const int N = NumberOfNodes();
  const int ID = MyNodeId();

  int len = (n + N - 1) / N;
  int beg = min(ID * len, n);
  int end = min((ID + 1) * len, n);
  if (beg == end) {
    PutChar(0, 'f');
    // cerr << ID << ": " << "fail" << endl;
  } else {
    PutChar(0, 'w');
    int sum = 0;
    int bestEnd = 0;
    int bestStart = 0;
    for (int i = beg; i < end; ++i) {
      int e = ElementAt(i+1);
      sum += e;
      bestEnd = max(bestEnd, sum);
      bestStart = min(bestStart, sum);
    }
    bestStart = sum - bestStart;
    int bestMid = sum;
    PutInt(0, bestStart);
    PutInt(0, bestMid);
    PutInt(0, bestEnd);
    // cerr << ID << ": " << "win: " << bestStart << ", " << bestMid << ", " << bestEnd << endl;
  }
  Send(0);
  if (ID == 0) {
    int bestStart[N];
    int bestMid[N];
    int bestEnd[N];
    int liveNodes = N;
    for (int i = 0; i < N; ++i) {
      Receive(i);
      if ('w' == GetChar(i)) {
        bestStart[i] = GetInt(i);
        bestMid[i] = GetInt(i);
        bestEnd[i] = GetInt(i);
      } else {
        liveNodes = i;
        break;
      }
    }
    int best = 0;
    int current = 0;
    for (int i = 0; i < liveNodes; ++i) {
      best = max(best, current + bestEnd[i]);
      current = max(current + bestMid[i], bestStart[i]);
    }
    best = max(best, current);
    cout << best << endl;
  }
  return 0;
}