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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "message.h"
#include "maklib.h"
#include <iostream>

long long left = 0, right = 0, inside = 0, all = 0;

int MyElementAt(int idx)
{
	return ElementAt(idx+1);
}

void calculate(int b, int e)
{
	long long aleft = 0, aright = 0, ainside = 0;
	int e2 = e-1;
	for(int i = b; i<e; ++i)
	{
		int v = MyElementAt(i);
		int v2 = MyElementAt(e-i);
		aleft += v;
		if (v>0 && aleft > left)
			left = aleft;
		aright += v2;
		if (v2 > 0 && aright > right) right = aright;
		
		ainside += v;
		if (ainside < 0) ainside = 0;
		else if (v > 0 && ainside > inside) inside = ainside; 
	}
	all = aleft;
}

int config_b = 0;
int config_e = 0;

void sendConfig(int idx, int b, int e)
{
	if (idx == 0)
	{
		config_b = b;
		config_e = e;
		return;
	}

	PutInt(idx, b);
	PutInt(idx, e);
	Send(idx);
}

void receiveConfig()
{
	Receive(0);
	config_b = GetInt(0);
	config_e = GetInt(0);
}

void splitJobs(int count, int nodes)
{
	int maxx = (int)((double)count/(double)nodes);
	int start = 0;
	for(int i = 0; i<nodes-1; ++i)
	{	
		sendConfig(i, start, start+maxx);
		start += maxx;
	}
	sendConfig(nodes-1, start, count);
}

void sendResult()
{
	PutLL(0, right);
	PutLL(0, left);
	PutLL(0, inside);
	PutLL(0, all);
}


struct Data {
	long long left_;
	long long right_;
	long long inside_;
	long long all_;
	
	Data() : left_(0), right_(0), inside_(0), all_(0) {}
};

Data *config;

void receiveResult(int nodes)
{
	config[0].left_ = left;
	config[0].right_ = right;
	config[0].inside_ = inside;
	config[0].all_ = all;
	for(int i=1;i<nodes;++i)
	{
		Receive(i);
		config[i].left_ = GetLL(i);
		config[i].right_ = GetLL(i);
		config[i].inside_ = GetLL(i);
		config[i].all_ = GetLL(i);
	}
}


long long calculate(int nodes)
{
	long long max_sum = 0, sum = 0;

	for(int i=0;i<nodes;++i)
	{
		if (sum + config[i].left_ > max_sum)
			max_sum = sum + config[i].left_;
		sum += config[i].all_;
		if (sum < config[i].right_)
			sum = config[i].right_;
		if (sum < 0) sum = 0;
		if (sum > max_sum)
			max_sum = sum;

		if (config[i].inside_ > max_sum)
			max_sum = config[i].inside_;
	}

	sum = 0;
	for(int i=nodes-1;i>=0;--i)
	{
		if (sum + config[i].right_ > max_sum)
			max_sum = sum + config[i].right_;
		sum += config[i].all_;
		if (sum < config[i].left_)
			sum = config[i].left_;
		if (sum < 0) sum = 0;
		if (sum > max_sum)
			max_sum = sum;
	}
	return max_sum;
}

int main()
{
	int s = Size();
	int node = MyNodeId();
	int nodes = NumberOfNodes();
	if (node == 0)
	{
		if (s == 0)
		{
			std::cout << "0" << std::endl;
			return 0;
		}
		if (s == 1)
		{
			int elem = MyElementAt(0);
			std::cout << (elem<0?0:elem)<<std::endl;
			return 0;
		}
		if (s == 2)
		{
			std::cout << std::max(std::max(MyElementAt(1), MyElementAt(2)), std::max(0, MyElementAt(1) + MyElementAt(2))) << std::endl;
			return 0;
		}
		if (s < 1000)
		{
			calculate(0, s);	
			std::cout << std::max(std::max(left, right), std::max(inside, all)) << std::endl;
			return 0;
		}
		config = new Data[nodes];	
		splitJobs(s, nodes);
		calculate(config_b, config_e);
		receiveResult(nodes);
		std::cout << calculate(nodes) << std::endl;
		delete[] config;
	}
	else
	{
		if (s < 1000)
			return 0;

		receiveConfig();
		calculate(config_b, config_e);
		sendResult();
	}
	return 0;
}