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
#include <bits/stdc++.h>
#include "message.h"
#include "kanapka.h"
#define FOR(i, a, b)	for (int (i)=(a); (i)<(b); (i)++)
#define PPC(x)			__builtin_popcountll((x))
#define ALL(x)			(x).begin(), (x).end()
#define pb				push_back
using namespace std;

void remin(long long& a, long long b)
{	a = min(a, b);	}

void remax(long long& a, long long b)
{	a = max(a, b);	}

int main()
{
	int Nds = NumberOfNodes(), ja = MyNodeId(), p = GetN() / Nds;
	int Beg = ja * p, End = ja == Nds-1 ? GetN() : Beg + p;
	long long res = 0, mn = 0, mx = 0, sum = 0;
	
	FOR(i, Beg, End)
	{
		sum += GetTaste(i);
		remin(res, sum - mx);
		remin(mn, sum);
		remax(mx, sum);
	}
	
	for (int m=1; m<Nds; m*=2)
	{
		int other = ja ^ m;
		if (ja & m)
		{
			PutLL(other, res);
			PutLL(other, mn);
			PutLL(other, mx);
			PutLL(other, sum);
			Send(other);
			return 0;
		}
		
		if (other >= Nds)
			continue;
		
		Receive(other);
		remin(res, GetLL(other));
		
		long long hismin = GetLL(other) + sum;
		remin(res, hismin - mx);
		remin(mn, hismin);
		
		remax(mx, GetLL(other) + sum);
		sum += GetLL(other);
	}
	
	res = sum - res;
	printf("%lld\n", res);	
	return 0;
}