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
//
//  Created by Kamil Borzym on 26/09/15.
//  Copyright © 2015 kam800. All rights reserved.
//

#import "stdio.h"
#import "kanapka.h"
#import "message.h"

typedef long long Taste;
typedef long long Idx;

// 1 ≤ N ≤ 5 * 10^8
// n in [−10^9, 10^9]
// maxN * n = 5 * 10^17 < 2^64

int main() {
    Idx n = GetN();

    Idx l = n * MyNodeId() / NumberOfNodes();
    Idx r = n * (MyNodeId()+1) / NumberOfNodes();

    Taste total = 0;
    Taste max = 0;
    Taste min = 0;
    Taste maxLoss = 0;

    Idx idx = l;
    while (idx < r) {
        Taste current = GetTaste(idx);
        total += current;
        if (total > max) {
            max = total;
        }
        if (total < min) {
            min = total;
        }
        Taste currentLoss = max - total;
        if (currentLoss > maxLoss) {
            maxLoss = currentLoss;
        }
        ++idx;
    }

    if (MyNodeId() > 0) {
        PutLL(0, total);
        PutLL(0, max);
        PutLL(0, min);
        PutLL(0, maxLoss);
        Send(0);
    } else {
        int node = 1;
        while (node < NumberOfNodes()) {
            Receive(node);
            Taste nTotal = GetLL(node);
            Taste nMax = GetLL(node);
            Taste nMin = GetLL(node);
            Taste nMaxLoss = GetLL(node);

            if (nMaxLoss > maxLoss) {
                maxLoss = nMaxLoss;
            }
            if (max - (total + nMin) > maxLoss) {
                maxLoss = max - (total + nMin);
            }
            if (total + nMax > max) {
                max = total + nMax;
            }
            total += nTotal;

            ++node;
        }
        Taste answer = total + maxLoss;
        printf("%lld\n", answer);
    }

    return 0;
}