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

#include <iostream>

using namespace std;

int main(){
  long long N = GetN();
  int nodes = NumberOfNodes();
  int myId = MyNodeId();
  long long pices = N / nodes; // !!!
  long long begin = myId * pices ;
  long long end = begin + pices - 1;
  if (myId == nodes-1) end = N-1;
//  if (end>N) end = N;
//  cout << "pices = " << pices << endl;
//  cout << "N = "<<N<<"\n";
//  cout << "begin = " << begin << endl;
//  cout << "end = " << end << endl;
  
  // szukamy w przedziale [begin,end]
  long long sum = 0;
  long long best = 0 , best_l = 0, best_r = 0;
  long long cur = 0;
  for (long long i = begin;i<=end;++i){
    long long x = GetTaste(i);
//    cout << " i = " << i << " x = " << x << endl;
    sum += x;
    if (cur + x < 0) {
      cur +=x;
      if (cur < best) best = cur;
    }
    else{
      cur = 0;
    }
    if (sum < best_l) best_l = sum;
  }
  if (myId > 0) {
//    cout << "Sending mess " << myId << endl;
    PutLL(0,best);   // mid
    PutLL(0,best_l); // left
    PutLL(0,sum);    // sum
    PutLL(0,cur);    // right
    Send(0);
  }
  else {
//    cout << " Receiving mesg " << myId << endl;
    long long suf = cur;
    for(int i=1;i<nodes;++i){
      Receive(i);
      long long mid = GetLL(i);
      if(mid < best) best = mid;
      long long left = GetLL(i);
      if (suf+left < best) best = suf+left;
      long long s = GetLL(i);
      suf += s; // sum
      sum += s;
      long long right = GetLL(i);
      if (right < best) best = right;
      if (suf >= 0)
        suf = right;
    }
    cout << sum - best << endl;
  }
  
//  cout << best <<endl;
//  cout << best_l << endl;
//  cout << cur << endl;
  
  
  
  return 0;
}