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
/*#ifdef _MSC_VER
  #ifndef __GNUC__
    #pragma warning(disable: 4996)
  #endif
  #define main main0
#endif*/
#include "kanapka.h"
#include "message.h"
#include <iostream>
using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  //cin.tie(NULL);
  int numberOfNodes = NumberOfNodes();
  int myNodeId = MyNodeId();
  long long s = GetN() / numberOfNodes;
  long long minTaste = s * myNodeId;
  long long maxTaste = s * (myNodeId + 1);
  if(myNodeId == numberOfNodes - 1)
    maxTaste = GetN();
  long long minValue = 0;
  long long maxValue = 0;
  long long sum = 0;
  long long difference = 0;

  for(long long i = minTaste; i < maxTaste; ++i) {
    sum += GetTaste(i);
    if(maxValue < sum)
      maxValue = sum;
    else {
      if(minValue > sum)
        minValue = sum;
      if(difference < maxValue - sum)
        difference = maxValue - sum;
    }
  }

  if(myNodeId > 0) {
    Receive(--myNodeId);
    long long prevSum = GetLL(myNodeId);
    long long prevMaxValue = GetLL(myNodeId);
    long long prevDiffrence = GetLL(myNodeId);
    long long diff = prevMaxValue - prevSum - minValue;
    if(difference < diff)
      difference = diff;
    if(difference < prevDiffrence)
      difference = prevDiffrence;
    maxValue += prevSum;
    if(maxValue < prevMaxValue)
      maxValue = prevMaxValue;
    sum += prevSum;
    ++myNodeId;
  }

  if(++myNodeId < numberOfNodes) {
    PutLL(myNodeId, sum);
    PutLL(myNodeId, maxValue);
    PutLL(myNodeId, difference);
    Send(myNodeId);
  } else
    cout << sum - difference << endl;
  return 0;
}