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

#include <algorithm>
#include <iostream>
using namespace std;

int nodeNo;
int nodeCount;

long long N;
long long A;
long long B;

long long bestSoFarA;
long long bestSoFarB;
long long sumSoFarA;
long long sumSoFarB;
long long bestA;
long long bestB;
long long bestAB;
long long sumIn;

const long long MAX_N = 5*100*1000*1000;
const long long FRAGMENT = 50;
const long long MAX_NODES = 10;
const long long MAX_PART = MAX_N/MAX_NODES;
const long long MAX_FRAGMENTS = (MAX_PART+FRAGMENT)/FRAGMENT;

// long long bestPartialA[MAX_FRAGMENTS];
// long long bestPartialB[MAX_FRAGMENTS];



void calculateSums() {
  long long sum = 0;
  bestA = 0;
  long long* bestSoFar = B > A ? new long long[B-A] : nullptr;
  for (int i = A; i < B; i++) {
    bestSoFar[i - A] = bestA;
    long long t = GetTaste(i);
    sum += t;
    if (sum > bestA) {
      bestA = sum;
    }
    
  }
  sumIn = sum;

  sum = 0; bestB = 0;
  bestAB = 0;
  for (int i = B-1; i >= A; i--) {
    long long t = GetTaste(i);
    sum += t;
    if (sum > bestB) {
      bestB = sum;
    }
    if (bestSoFar[i - A] + bestB > bestAB) {
      bestAB = bestSoFar[i - A] + bestB;
    }
  }
  if (bestSoFar) delete[] bestSoFar;
}


void transferSums() {
  if (nodeNo > 0) {
    Receive(nodeNo - 1);
    bestSoFarA = GetLL(nodeNo - 1);
    sumSoFarA = GetLL(nodeNo - 1);
    // cerr << nodeNo << ") bestSoFarA=" << bestSoFarA << " sumSoFarA=" << sumSoFarA << endl;
  } else {
    bestSoFarA = 0;
    sumSoFarA = 0;
  }

  if (nodeNo + 1 < nodeCount) {
    PutLL(nodeNo + 1, max(bestSoFarA, sumSoFarA + bestA));
    PutLL(nodeNo + 1, sumSoFarA + sumIn);
    Send(nodeNo + 1);
  }

  if (nodeNo + 1 < nodeCount) {
    Receive(nodeNo + 1);
    bestSoFarB = GetLL(nodeNo + 1);
    sumSoFarB = GetLL(nodeNo + 1);
  } else {
    bestSoFarB = 0;
    sumSoFarB = 0;
  }

  if (nodeNo > 0) {
    PutLL(nodeNo - 1, max(bestSoFarB, sumSoFarB + bestB));
    PutLL(nodeNo - 1, sumSoFarB + sumIn);
    Send(nodeNo - 1);
  }
}


void calculateResult() {
  long long bestExternal = bestSoFarA + bestSoFarB;
  long long bestInternalA = sumSoFarA + bestA + bestSoFarB;
  long long bestInternalB = bestSoFarA + bestB + sumSoFarB;
  long long bestInternalAB = sumSoFarA + bestAB + sumSoFarB;
  
  long long best = max(max(bestExternal, bestInternalA), max(bestInternalB, bestInternalAB));

  long long passed;

  if (nodeNo > 0) {
    Receive(nodeNo - 1);
    passed = GetLL(nodeNo - 1);
  } else {
    passed = 0;
  }

  best = max(passed, best);

  if (nodeNo + 1 < nodeCount) {
    PutLL(nodeNo + 1, best);
    Send(nodeNo + 1);
  } else {
    cout << best << endl;
  }
}


int main() {
  N = GetN();

  nodeCount = NumberOfNodes();
  nodeNo = MyNodeId();

  A = ((N + nodeCount - 1) / nodeCount) * nodeNo;
  B = ((N + nodeCount - 1) / nodeCount) * (nodeNo + 1);
  if (B > N) B = N;

  calculateSums();
  transferSums();
  // cerr << nodeNo << " [" << A << ", " << B << "]: " << sumSoFarA << " " << sumSoFarB << " " << sumIn << " | " << bestA << " " << bestB << endl;
  calculateResult();

  return 0;
}