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


#include <algorithm>
#include <iostream>

using namespace std;

int main() {

	long long n = GetN();
	int p = NumberOfNodes();
	int currentNode = MyNodeId();
  	long long start = currentNode * n / p;
  	long long end = (currentNode + 1) * n / p;

  	long long bestRight = 0;
  	long long indexRight = 0;
  	long long currentRight = 0;
  	for (long long i = start; i < end; ++i)
  	{
  		currentRight += GetTaste(i);
  		if (currentRight > bestRight)
  		{
  			bestRight = currentRight;
  			indexRight = i;
  		}
  	}

  	long long bestLeft = 0;
	long long indexLeft = 0;
	long long currentLeft = 0;
  	if (currentNode)
  	{
  		PutLL(0, bestRight);
  		PutLL(0, indexRight);

	  	PutLL(0, currentRight);
	  	PutLL(0, end - 1);
	  	
	  	Send(0);

	  	Receive(0);

	  	long long minIndex = GetLL(0);
	  	for (long long i = end - 1; i >= start && i > minIndex; --i)
	  	{
	  		currentLeft += GetTaste(i);
	  		if (currentLeft > bestLeft)
	  		{
	  			bestLeft = currentLeft;
	  			indexLeft = i;
	  		}
	  	}

	  	PutLL(0, bestLeft);
	  	PutLL(0, currentLeft);
	  	
	  	Send(0);
  	}
  	else
  	{
  		for(int i = 1; i < p; ++i)
  		{
  			Receive(i);
  			long long message = GetLL(i);
  			long long index = GetLL(i);
  			currentRight += message;
  			if (currentRight > bestRight)
  			{
  				bestRight = currentRight;
  				indexRight = index;
  			}
  			
  			currentRight += GetLL(i) - message;
  			index = GetLL(i);
  			if (currentRight > bestRight)
  			{
  				bestRight = currentRight;
  				indexRight = index;
  			}

  			
  		}

  		for(int i = 1; i < p; ++i)
  		{
  			PutLL(i, indexRight);
  			Send(i);
  		}

  		for (int i = p - 1; i > 0; --i)
  		{
  			Receive(i);
  			long long message = GetLL(i);
  			currentLeft += message;
  			if (currentLeft > bestLeft)
  			{
  				bestLeft = currentLeft;
  			}
  			
  			currentLeft += GetLL(i) - message;
  			if (currentLeft > bestLeft)
  			{
  				bestLeft = currentLeft;
  			}
  		}

  		for (long long i = end - 1; i >= start && i > indexRight; --i)
	  	{
	  		currentLeft += GetTaste(i);
	  		if (currentLeft > bestLeft)
	  		{
	  			bestLeft = currentLeft;
	  			indexLeft = i;
	  		}
	  	}
  		cout << bestRight + bestLeft << endl;
  	}
  return 0;
}