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
#include "message.h"
#include "kanapka.h"
#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long ll;

int main() {

  int nodesCount = NumberOfNodes();
  int myId = MyNodeId();

  ll n = GetN();

  ll intervalLength = n / nodesCount;
  if(intervalLength * nodesCount < n) {
    intervalLength++;
  }

  ll a = myId * intervalLength + 1;
  ll b = min(a + intervalLength - 1, n);

  ll localMax = 0;
  ll localSum = 0;
  for(ll i = a; i <=b; i++) {
    localSum += GetTaste(i);
    if(localMax < localSum) {
      localMax = localSum;
    }
  }

  ll previousSum, previousMax;
  ll globalSum, globalMax;
  if(myId > 0) {
    Receive(myId - 1);
    previousSum = GetLL(myId - 1);
    previousMax = GetLL(myId - 1);
    globalSum = previousSum + localSum;
    globalMax = max(previousMax, previousSum + localMax);
  } else {
    previousSum = 0;
    previousMax = 0;
    globalSum = localSum;
    globalMax = localMax;
  }

  if(myId < nodesCount-1) {
    PutLL(myId + 1, globalSum);
    PutLL(myId + 1, globalMax);
    Send(myId + 1);
  }

////  printf("%d: %d-%d: pS, pM, gS, gM: %lld, %lld, %lld, %lld\n",
////         myId, a, b, previousSum, previousMin, globalSum, globalMin);

  ll actualLocalSum = previousSum;
  ll actualLocalMax = previousMax;
  ll actualLocalMin = 0;

  for(ll i = a; i <= b; i++) {
    actualLocalSum += GetTaste(i);
    if(actualLocalMax < actualLocalSum) {
      actualLocalMax = actualLocalSum;
    }
    if(actualLocalSum - actualLocalMax < actualLocalMin) {
      actualLocalMin = actualLocalSum - actualLocalMax;
    }
  }

  if(myId > 0) {
    PutLL(0, actualLocalMin);
//    if(myId == nodesCount - 1) {
//      PutLL(0, globalSum);
//    }
    Send(0);
  }

  if(myId == 0) {
    ll actualGlobalMin = actualLocalMin;
    ll realGlobalSum = 1000;
    for(ll i = 1; i < nodesCount; i++) {
      Receive(i);
      ll minOfI = GetLL(i);
      if(minOfI < actualGlobalMin) {
        actualGlobalMin = minOfI;
      }
//      if(i == nodesCount - 1) {
//        actualGlobalMin = GetLL(i);
//      }
    }

    printf("%lld\n", realGlobalSum - actualGlobalMin);
  }

  return 0;
}