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
#ifdef _MSC_VER
#pragma warning(disable: 4996)
#endif

#include "message.h"
#include "maklib.h"
#include <iostream>
using namespace std;

void Calculate(long long& result, long long& current, int first, int last) {
  for(int i = first; i < last; ++i) {
    if(current > 0)
      current += ElementAt(i);
    else
      current =  ElementAt(i);
    if(result < current)
      result = current;
  }
}

int main() {
  int id = MyNodeId();
  int nodes = NumberOfNodes();
  int size = Size();

  int first = static_cast<long long>(id) * size / nodes + 1;
  int last = static_cast<long long>(id + 1) * size / nodes + 1;

  long long result = 0;
  long long current = 0;

  if(id > 0) {
    int source = Receive(id - 1);
    current = GetLL(source);
    result = GetLL(source);
  }

  Calculate(result, current, first, last);

  if(last > size)
    cout << result << '\n';
  else {
    PutLL(id + 1, current);
    PutLL(id + 1, result);
    Send(id + 1);
  }

  return 0;
}