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
#include "kanapka.h"
#include "message.h"
#include <bits/stdc++.h>

using namespace std;

long long int MinSubarray(vector<long long int>& vec)
{
	long long int sum = 0;
	long long int res = 0;
	for (int i = 0; i < vec.size(); ++i)
	{
		sum += vec[i];
		res = min(res, sum);
		if (sum > 0)
		{
			sum = 0;
		}
	}
	return res;
}

int main()
{
	{
		long long int l = 0, r = 0, s = 0, ms = 0;
		long long int lv = 0, rv = 0, msv = 0;
		long long int a, b;
		a = (MyNodeId() * GetN()) / NumberOfNodes();
		b = ((MyNodeId() + 1) * GetN()) / NumberOfNodes();
		for (int i = a; i < b; ++i)
		{
			lv += GetTaste(i);
			l = min(l, lv);
			s += GetTaste(i);
			msv += GetTaste(i);
			ms = min(ms, msv);
			if (msv > 0)
			{
				msv = 0;
			}
		}
		for (int i = b - 1; i >= a; --i)
		{
			rv += GetTaste(i);
			r = min(r, rv);
		}
		PutLL(0, l);
		PutLL(0, r);
		PutLL(0, s);
		PutLL(0, ms);
		Send(0);
	}
	if(MyNodeId() == 0)
	{
		vector<long long int> vec(2 * NumberOfNodes());
		vector<long long int> beg(NumberOfNodes());
		long long int sum = 0;
		long long int mss = 0;
		for (int parts = 0; parts < NumberOfNodes(); ++parts)
		{
			int ins = Receive(-1);
			long long int l = 0, r = 0, s = 0;
			l = GetLL(ins);
			r = GetLL(ins);
			s = GetLL(ins);
			mss = min(mss, GetLL(ins));
			vec[ins * 2] = l;
			vec[ins * 2 + 1] = s - l;
			beg[ins] = r;
			sum += s;
		}
		long long int val = beg[0];
		long long int res = val;
		for (int i = 2; i < vec.size(); ++i)
		{
			val += vec[i];
			res = min(res, val);
			if (val > 0)
			{
				val = beg[i / 2];
			}
		}
		cout << sum - min(res, mss) << endl;
	}
	return 0;
}