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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <utility>
#include <stack>

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define MP make_pair
#define FOR(v,p,k) for(int v=(p);v<=(k);++v)
#define FORD(v,p,k) for(int v=(p);v>=(k);--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()

#define ODD(x) ((x)%2)
#define EVEN(x) (!(ODD(x)))

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

class SegmentParams {
  LL maxPrefixSum, maxSubSum, maxSuffixSum, sum;

public:
  SegmentParams() : maxPrefixSum(0), maxSubSum(0), maxSuffixSum(0), sum(0) {}

  void printMaxSubSum() {
    printf("%lld\n", maxSubSum);
  }

  void extendRight(LL value) {
    sum = sum + value;
    maxPrefixSum = max(maxPrefixSum, sum);
    maxSuffixSum = max(0LL, maxSuffixSum + value);
    maxSubSum = max(maxSubSum, maxSuffixSum);
  }

  void extendRight(const SegmentParams &other) {
    maxSubSum = max(max(maxSubSum, other.maxSubSum), 
                    maxSuffixSum + other.maxPrefixSum);
    maxPrefixSum = max(maxPrefixSum, sum + other.maxPrefixSum);
    maxSuffixSum = max(maxSuffixSum + other.sum, other.maxSuffixSum);
    sum = sum + other.sum;
  }

  void send() {
    PutLL(0, maxPrefixSum);
    PutLL(0, maxSubSum);
    PutLL(0, maxSuffixSum);
    PutLL(0, sum);
    Send(0);
  }

  void fillFromNode(int nodeID) {
    maxPrefixSum = GetLL(nodeID);
    maxSubSum = GetLL(nodeID);
    maxSuffixSum = GetLL(nodeID);
    sum = GetLL(nodeID);
  }
};


int main() {
  int nodesNumber = NumberOfNodes();
  int nodeID = MyNodeId();
  int n = Size();

  int elementsPerNode = n/nodesNumber;
  int firstElementPos = 1 + nodeID*elementsPerNode;
  int lastElementPos = firstElementPos + elementsPerNode - 1;
  if (nodeID == nodesNumber-1) {
    lastElementPos = n;
  }

  VI nodeElements;
  int nodeElementsSize = lastElementPos - firstElementPos + 1;
  nodeElements.reserve(nodeElementsSize);
  FOR(pos, firstElementPos, lastElementPos) {
    nodeElements.PB(ElementAt(pos));
  }

  SegmentParams nodeSegmentParams;
  REP(i, nodeElementsSize) {
    nodeSegmentParams.extendRight(nodeElements[i]);
  }

  if (nodeID > 0) {
    nodeSegmentParams.send();
  } else {
    vector<SegmentParams> allSegmentParams(nodesNumber);
    allSegmentParams[0] = nodeSegmentParams;

    REP(i, nodesNumber-1) {
      int receivedNodeID = Receive(-1);
      allSegmentParams[receivedNodeID].fillFromNode(receivedNodeID);
    }

    SegmentParams accumulatedSegmentParams = nodeSegmentParams;
    FOR(otherNodeID, 1, nodesNumber-1) {
      accumulatedSegmentParams.extendRight(allSegmentParams[otherNodeID]);
    }
    accumulatedSegmentParams.printMaxSubSum();
  }
  return 0;
}