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
#include <iostream>
#include "message.h"
#include "kanapka.h"
using namespace std;
typedef long long LL;
struct {
    int total, id;
} Node;

int n;
int from, to;
int main()
{
    Node.total = NumberOfNodes();
    Node.id = MyNodeId();

    n = GetN();
    from = Node.id * ((n + Node.total - 1) / Node.total);
    to = min((Node.id + 1) * ((n + Node.total - 1) / Node.total), n) - 1;
    LL sum = 0;
    LL localSol = 0;
    LL ma = 0;
    LL mi = 0;
    for (int i = from; i <= to; i++) {
        int el = GetTaste(i);
        sum += el;
        localSol = min(localSol, sum - ma);
        ma = max(ma, sum);
        mi = min(mi, sum);
    }

    LL prevSum = 0;
    LL prevRes = 0;
    LL prevMax = 0;
    if (Node.id > 0) {
        Receive(Node.id - 1);
        prevSum = GetLL(Node.id - 1);    
        prevRes = GetLL(Node.id - 1);
        prevMax = GetLL(Node.id - 1);
    }
    
    LL newRes = min(localSol, mi + prevSum - prevMax);
    newRes = min(newRes, prevRes);
    LL newSum = sum + prevSum;
    LL newMax = max(prevMax, prevSum + ma);
    if (Node.id + 1 < Node.total) {
        PutLL(Node.id + 1, newSum); 
        PutLL(Node.id + 1, newRes); 
        PutLL(Node.id + 1, newMax);
        Send(Node.id + 1);
        Receive(Node.id + 1);
        newSum = GetLL(Node.id + 1); 
        newRes = GetLL(Node.id + 1); 
        newMax = GetLL(Node.id + 1); 
    }
    if (Node.id > 0) {
        PutLL(Node.id - 1, newSum);
        PutLL(Node.id - 1, newRes);
        PutLL(Node.id - 1, newMax);
        Send(Node.id - 1);
    }
    if (Node.id == 0) {
        cout << (newSum - newRes) << endl; 
    }

    
    return 0;
}