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
#include <iostream>
#include "bits/stdc++.h"
#include "message.h"
#include "teatr.h"
using namespace std;

int e[6];
int nextE[6];
long long int brawls;

int main() {
    int n = GetN();
    int k = MyNodeId();
    if(n < 100) {
        for(int i = 0; i < n; i++) {
            int element = GetElement(i);
            e[element]++;
            for(int j = element+1; j <= 5; j++)
                brawls += e[j];
        }
    } else {
        if(k < 99) {
            for(int i = k*(n/100); i < (k+1)*(n/100); i++) {
                int element = GetElement(i);
                e[element]++;
                for(int j = element+1; j <= 5; j++)
                    brawls += e[j];
            }
        } else {
            for(int i = 99*(n/100); i < n; i++) {
                int element = GetElement(i);
                e[element]++;
                for(int j = element+1; j <= 5; j++)
                    brawls += e[j];
            }
        }

        if(k == 0) {
            Receive(k+1);
            for(int i = 1; i <= 5; i++) {
                nextE[i] = GetInt(k+1);
            }

            int nextSum = nextE[1] + nextE[2] + nextE[3] + nextE[4] + nextE[5];

            for(int i = 5; i>= 2; i--) {
                nextSum -= nextE[i];

                brawls += nextSum * e[i];
            }

            brawls += GetLL(k+1);
        } else {
            if(k != 99) {
                Receive(k+1);
                for(int i = 1; i <= 5; i++) {
                    nextE[i] = GetInt(k+1);
                }
                int nextSum = nextE[1] + nextE[2] + nextE[3] + nextE[4] + nextE[5];

                for(int i = 5; i>= 2; i--) {
                    nextSum -= nextE[i];

                    brawls += nextSum * e[i];
                }

                for(int i = 1; i <= 5; i++) {
                    e[i] += nextE[i];
                }
            }

            for(int i = 1; i <= 5; i++) {
                PutInt(k-1, e[i]);
            }

            if(k != 99) {
                brawls += GetLL(k+1);
            }
            PutLL(k-1, brawls);

            Send(k-1);
        }
    }

    if(k == 0) {
        cout << brawls << endl;
    }

    return 0;
}