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
// Potyczki algorytmiczne 2015
// Runda próbna - Kanapka
// Magdalena Szarkowska

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

int main() {
	
	ios_base::sync_with_stdio(0);
	
	long long n = GetN();                   
	long long nodes = NumberOfNodes();      
	long long len = (n+nodes-2)/(nodes-1);  
	
	long long sum = 0;
	long long pref = 0, minPref = 0, maxPref = 0;
	long long minSuf = 0;
	long long minMid = 0, minPom = 0;
	
	int no = MyNodeId();
	long long int taste;
	
	if(no > 0) {
		
		for(int i = (no-1)*len; i <= min(no*len-1, n-1); i++) {
			taste = GetTaste(i);
			sum = sum + taste;

			pref = pref + taste;
			minPref = min(minPref, pref);
			maxPref = max(maxPref, pref);

			if(minPom <= 0) minPom = minPom + taste;
			else minPom = taste;
			minMid = min(minPom, minMid);
		}
		minSuf = sum - maxPref;
		
		PutLL(0, sum);
		PutLL(0, minPref);
		PutLL(0, minSuf);
		PutLL(0, minMid);
		Send(0);
	}
	else {
		
		long long tab[3*nodes];
		long long sumAll = 0, minMinMid = 0;
		
		for(int i = 1; i < nodes; i++) {
			
			Receive(i);
			sum = GetLL(i);
			minPref = GetLL(i);
			minSuf = GetLL(i);
			minMid = GetLL(i);
			
			tab[3*i-3] = minPref;
			tab[3*i-2] = sum - minPref - minSuf;
			tab[3*i-1] = minSuf;
			
			sumAll = sumAll + sum;
			minMinMid = min(minMinMid, minMid);
		}
		
		long long sandwichIsSoTasty = max(0LL, sumAll-minMinMid);
		
		long long int result = 0, minResult = 0;
		
		for(int i = 0; i < 3*(nodes-1); i++) {
			if(result <= 0) result = result + tab[i]; 
			else result = tab[i];
			minResult = min(result, minResult);
		}
		
		sandwichIsSoTasty = max(sandwichIsSoTasty, sumAll-minResult);
		cout << sandwichIsSoTasty;
	}
	
	return 0;
}