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
// Piotr Golda

#include "message.h"
#include "kanapka.h"
#include <algorithm>
#include <iostream>
#include <type_traits>
using namespace std;

const auto N = GetN();
using INT_T = std::remove_reference< std::remove_const<decltype(N)>::type >::type;

const INT_T Nodes = NumberOfNodes();
const INT_T ID = MyNodeId();

static inline INT_T ComputeStart(INT_T id)
{
	return (N / Nodes)*id + std::min(N%Nodes, id);
}

static inline INT_T ComputeEnd(INT_T id, INT_T start)
{
	return start + (N / Nodes) + ((id < N%Nodes) ? 1 : 0);
}

const auto Start = ComputeStart(ID);
const auto End = ComputeEnd(ID, Start);

void ComputePrefixes(INT_T& maxPrefix, INT_T& sum, INT_T& minSubSequence, INT_T& minSum)
{
	maxPrefix = { 0 };
	sum = { 0 };
	minSubSequence = { 0 };
	minSum = { 0 };
	for (auto i = Start; i < End; ++i)
	{
		sum += GetTaste(i);
		minSubSequence = std::min(minSubSequence, sum - maxPrefix);
		if (maxPrefix < sum)
		{
			maxPrefix = sum;
			minSum = sum;
		}
		minSum = std::min(minSum, sum);
	}
}


int main()
{
	INT_T maxPrefix{ 0 };
	INT_T sum{ 0 };
	INT_T minSubSequence{ 0 };
	INT_T minSum{ 0 };
	ComputePrefixes(maxPrefix, sum, minSubSequence, minSum);
	if (ID > 0)
	{
		PutLL(0, maxPrefix);
		PutLL(0, minSum);
		PutLL(0, sum);
		PutLL(0, minSubSequence);
		Send(0);
	}
	else
	{
		for (auto i = 1; i < Nodes; ++i)
		{
			Receive(i);

			const auto prefix = sum + GetLL(i);
			maxPrefix = std::max(maxPrefix, prefix);
			minSum = sum + GetLL(i);
			sum += GetLL(i);
			minSubSequence = std::min(minSubSequence, minSum - maxPrefix);
			minSubSequence = std::min(minSubSequence, GetLL(i));
		}
		std::cout << sum - minSubSequence << std::endl;
	}
}