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
#include "message.h"
#include "kanapka.h"
#include <vector>
#include <cstdio>

int main(int argc, char * argv[])
{
	long long int totalN = GetN();
	int nodes = NumberOfNodes();
	int node = MyNodeId();
	long long int testN = totalN / nodes;
	long long int start = testN * node;
	long long int end = start + testN;
	if(node == nodes - 1)
		end += totalN % nodes;

	long long int total_smak = 0ll;
	long long int max_smak = 0ll;

	for(long long int i = start; i < end; i++)
	{
		total_smak += GetTaste(i);
		if(total_smak > max_smak)
			max_smak = total_smak;
	}

	if(node != 0)
	{
		PutLL(0, total_smak);
		PutLL(0, max_smak);
		Send(0);
	}
	else
	{
		std::vector<long long int> total_smak_all;
		std::vector<long long int> max_smak_all;

		total_smak_all.push_back(total_smak);
		max_smak_all.push_back(max_smak);
		
		for(int i = 1; i < nodes; i++)
		{
			Receive(i);
			total_smak_all.push_back(GetLL(i));
			max_smak_all.push_back(GetLL(i));
		}
		
		long long int total_left = 0ll;
		long long int total_right = 0ll;
		long long int max_left = 0ll;
		long long int max_right = 0ll;
		
		int max_left_reached = nodes;
		int max_right_reached = nodes;
		
		for(int i = 0; i < nodes; i++)
		{
			if(total_left + max_smak_all[i] > max_left && i < max_right_reached)
			{
				max_left_reached = nodes - i - 1;
				max_left = total_left + max_smak_all[i];
			}
			if(total_right + max_smak_all[nodes - i - 1] > max_right && i < max_left_reached)
			{
				max_right_reached = nodes - i - 1;
				max_right = total_right + max_smak_all[nodes - i - 1];
			}
			total_left += total_smak_all[i];
			total_right += total_smak_all[nodes - i - 1];
		}
		
		if(max_left + max_right > total_left)
			printf("%ll", max_left + max_right);
		else
			printf("%ll", total_left);
	}

	return 0;
}