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
#include "kanapka.h"
#include "message.h"

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  {
    long long start = MyNodeId() * GetN() / NumberOfNodes();
    long long end = (MyNodeId() + 1) * GetN() / NumberOfNodes();
#ifdef DBG
    cout << "node: " << MyNodeId()
         << " start:" << start
         << " end:" << end
         << endl;
#endif

    long long sum = 0, best_inside = 0, best_right = 0, best_left = 0;
    for (long long i = start; i < end; ++i) {
      long long x = GetTaste(i);
      sum += x;
      best_right += x;
      if (best_right > 0) { best_right = 0; }
      best_left = min(best_left, sum);
      best_inside = min(best_inside, best_right);
    }
    PutLL(0, sum);
    PutLL(0, best_left);
    PutLL(0, best_inside);
    PutLL(0, best_right);
    Send(0);
#ifdef DBG
    cout << "node:" << MyNodeId()
         << " sum:" << sum
         << " best_left:" << best_left
         << " best_inside:" << best_inside
         << " best_right:" << best_right
         << endl;
#endif
  }

  if (MyNodeId() == 0) {
    long long best = 0;
    long long current = 0;
    long long overall_sum = 0;
    for (int i = 0; i < NumberOfNodes(); ++i) {
      Receive(i);
      long long sum = GetLL(i);
      long long best_left = GetLL(i);
      long long best_inside = GetLL(i);
      long long best_right = GetLL(i);
#ifdef DBG
      cout << "received from node:" << i
           << " sum:" << sum
           << " best_left:" << best_left
           << " best_inside:" << best_inside
           << " best_right:" << best_right
           << endl;
#endif
      overall_sum += sum;
      best = min(best, best_inside);
      best = min(best, current + best_left);
      current = min(current + sum, best_right);
    }
    cout << overall_sum - best << endl;
  }
}