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
#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(auto v=(p);v<=(k);++v)
#define FORD(v,p,k) for(auto v=(p);v>=(k);--v)
#define REP(i,n) for(auto 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 "kanapka.h"
#include "message.h"

class SegmentParams {
  LL minPrefixSum, minSubSum, minSuffixSum, sum;

public:
  SegmentParams() : minPrefixSum(0), minSubSum(0), minSuffixSum(0), sum(0) {}

  void printResult() {
    printf("%lld\n", sum - minSubSum);
  }

  void extendRight(LL value) {
    sum = sum + value;
    minPrefixSum = min(minPrefixSum, sum);
    minSuffixSum = min(0LL, minSuffixSum + value);
    minSubSum = min(minSubSum, minSuffixSum);
  }

  void extendRight(const SegmentParams &other) {
    minSubSum = min(min(minSubSum, other.minSubSum), 
                    minSuffixSum + other.minPrefixSum);
    minPrefixSum = min(minPrefixSum, sum + other.minPrefixSum);
    minSuffixSum = min(minSuffixSum + other.sum, other.minSuffixSum);
    sum = sum + other.sum;
  }

  void send() {
    PutLL(0, minPrefixSum);
    PutLL(0, minSubSum);
    PutLL(0, minSuffixSum);
    PutLL(0, sum);
    Send(0);
  }

  void fillFromNode(int nodeID) {
    minPrefixSum = GetLL(nodeID);
    minSubSum = GetLL(nodeID);
    minSuffixSum = GetLL(nodeID);
    sum = GetLL(nodeID);
  }
};




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

  int firstElementPos = n * nodeID / nodesNumber;
  int lastElementPos = n * (nodeID + 1) / nodesNumber - 1;

  SegmentParams nodeSegmentParams;
  FOR(pos, firstElementPos, lastElementPos) {
    nodeSegmentParams.extendRight(GetTaste(pos));
  }

  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.printResult();
  }
  return 0;
}