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

#include <algorithm>

using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
#define mp make_pair
#define pb push_back
#define st first
#define nd second

int nodes = NumberOfNodes();
int me = MyNodeId();
ll n = GetN();

struct worker_data {
	ll total, best_prefix, best_suffix, best_PF;
	void print() {
		printf("%lld, %lld, %lld, %lld\n", total, best_prefix, best_suffix, best_PF);
	}
};

struct master {
	
	int workers;
	
	void distributeIntervals() {
		workers = min(nodes - 1, int(n));
		//printf("%d\n", workers);
		int len = n / workers;
		for (int i = 1; i < workers; i++) {
			PutInt(i, (i-1) * len);
			PutInt(i, i * len - 1);
			//printf("%d: %d %d\n", i, (i-1) * len, i * len - 1);
			Send(i);
		}
		PutInt(workers, (workers-1) * len);
		PutInt(workers, n-1);
		Send(workers);
		for (int i = workers + 1; i < nodes; i++) {
			PutInt(i, -1);
			PutInt(i, -1);
			Send(i);
		}
	}
	
	vector<worker_data> D;
	vector<ll> best_prefix, sum, best_suffix;
	
	void collectData() {
		
		D.resize(workers+1);
		
		best_prefix.resize(workers+1);
		
		best_suffix.resize(workers+1);
		
		sum.resize(workers+1);
		
		ll out = 0LL;
		
		for (int i = 1; i <= workers; i++) {
			Receive(i);
			D[i].total = GetLL(i);
			D[i].best_prefix = GetLL(i);
			D[i].best_suffix = GetLL(i);
			D[i].best_PF = GetLL(i);
		}
		
		out = max(out, sum[workers]);
	
		for (int i = 1; i <= workers; i++) {
			//D[i].print();
			sum[i] = sum[i-1] + D[i].total;
			out = max(out, sum[workers] - D[i].total + D[i].best_PF);
			best_prefix[i] = max(best_prefix[i-1], sum[i-1] + D[i].best_prefix);
		}
		
		for (int i = workers; i >= 1; i--) {
			best_suffix[i] = max(best_suffix[i], 1LL);
			out = max(out, best_suffix[i] + best_prefix[i-1]);
		}
		
		printf("%lld\n", out);
	}
};

struct worker {
	int b, e, len;
	ll total;
/*
 * Format informacji od jednego workera:
 * LL: suma
 * LL: najlepszy prefiks
 * LL: najlepszy sufiks
 * LL: najlepszy zestaw prefiks + sufiks */
	
	void sendSum() {
		ll sum = 0;
		for (int i = 0; i < len; i++) {
			sum = sum + GetTaste(b + i);
		}
		total = sum;
		PutLL(0, sum);
	}
	
	void sendBestPrefix() {
		ll sum = 0;
		ll best_prefix = 0;
		for (int i = 0; i < len; i++) {
			sum = sum + GetTaste(b + i);
			best_prefix = max(best_prefix, sum);
		}
		PutLL(0, best_prefix);
	}
	
	void sendBestSuffix() {
		ll sum = 0;
		ll best_suffix = total;
		for (int i = len-1; i >= 0; i--) {
			sum = sum + GetTaste(b + i);
			best_suffix = max(best_suffix, sum);
		}
		PutLL(0, best_suffix);
	}
	
	void sendBestPF() {
		ll sum = 0, best_prefix = 0, best_PF = 0;
		for (int i = 0; i < len; i++) {
			sum = sum + GetTaste(b + i);
			best_prefix = max(best_prefix, sum);
			best_PF = max(best_PF, total - sum + best_prefix);
		}
		PutLL(0, best_PF);
	}
	
	void solveInterval() {
		Receive(0);
		b = GetInt(0);
		e = GetInt(0);
		if (b == -1 || e == -1) return;
		len = e-b+1;
		
		sendSum();
		sendBestPrefix();
		sendBestSuffix();
		sendBestPF();
		Send(0);
	}
};


int main() {
	if (me == 0) {
		master M;
		M.distributeIntervals();
		M.collectData();
	}
	else {
		worker W;
		W.solveInterval();
	}
	return 0;
}