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
#include<bits/stdc++.h>
#include"message.h"
#include"teatr.h"
using namespace std;
#ifndef d
#define d(...)
#endif
#define st first
#define nd second
#define pb push_back
#define siz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
typedef long long LL;
typedef long double LD;
constexpr int INF=1e9+7;
constexpr LL INFL=1e18;
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

struct socket  //source https://github.com/KubinGH/cpp-algorithmics-delta/blob/master/Common/KTL/util/zeus/socket.cpp
{
    uint32_t conn;
    socket(uint32_t _conn) : conn(_conn) {}
    socket& operator+ () { Receive(conn); return *this; }
    socket& operator- () { Send(conn); return *this; }
    #define BYTES(_b) template<typename T> typename enable_if<(_b) == sizeof(T), socket&>::type
    BYTES(1) operator<< (T val) { PutChar(conn, val); return *this; }
    BYTES(1) operator>> (T& var) { var = GetChar(conn); return *this; }
    BYTES(4) operator<< (T val) { PutInt(conn, val); return *this; }
    BYTES(4) operator>> (T& var) { var = GetInt(conn); return *this; }
    BYTES(8) operator<< (T val) { PutLL(conn, val); return *this; }
    BYTES(8) operator>> (T& var) { var = GetLL(conn); return *this; }
    #undef BYTES
};

int f[1000005],n,num,me;
constexpr int base=1000001;

inline int ask(int x) {
	int res=0;
	while(x>0) {
		res+=f[x];
		x-=x&(-x);
	}
	return res;
}
inline int add(int i) {
	while(i<=base) {
		--f[i];
		i+=i&(-i);
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	n=GetN(), num=NumberOfNodes(), me=MyNodeId();
	int from=me/10, to=me%10, cnt=0;
	if(to<=from) to+=10;
	LL res=0;
	for(int i=to;i<n;i+=10, from+=10) {
		add(GetElement(from));
		res+=ask(GetElement(i));
	}
	res+=((LL)from/10)*(from/10+1)/2;
	-(socket(0)<<res);
	
	if(me == 0) {
		LL ans=0,x;
		for(int i=0;i<num;i++) {
			(+socket(i))>>x;
			ans+=x;
		}
		cout<<ans<<"\n";
	}

	return 0;
}