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
#include <bits/stdc++.h>
#include "kanapka.h"
#include "message.h"

using namespace std;

const int MAX_NUMBER_OF_INSTANCES = 105;

long long sum[MAX_NUMBER_OF_INSTANCES],
          pref[MAX_NUMBER_OF_INSTANCES],
          suf[MAX_NUMBER_OF_INSTANCES];

int instanceId, numberOfInstances;
int n, elementsPerInstance;

long long bestSubInterval();
long long bestPrefix();
long long bestSuffix();
long long sumOfElements();
void sendResultToMainInstance(long long ans, long long bestPref, long long bestSuf, long long sum);
long long getResult();

int main() {
    instanceId = MyNodeId();
    numberOfInstances = NumberOfNodes();
    n = GetN();
    elementsPerInstance = (n + numberOfInstances - 1) / numberOfInstances;
    
    long long ans = bestSubInterval();
    long long bestPref = bestPrefix();
    long long bestSuf = bestSuffix();
    long long sum = sumOfElements();
    
    sendResultToMainInstance(ans, bestPref, bestSuf, sum);
    
    if (instanceId == 0) {
        printf("%lld\n", getResult());
    }
     
    return 0;
}


long long getResult() {
    long long res = 0;
    
    for (int i = 0; i < numberOfInstances; i++) {
        Receive(i);
        res = min(res, GetLL(i));
        pref[i] = GetLL(i);
        suf[i] = GetLL(i);
        sum[i] = (i > 0? sum[i - 1]: 0) + GetLL(i);
    }
    for (int i = 0; i < numberOfInstances; i++) {
        for(int j = i + 1; j < numberOfInstances; j++) {
            res = min(res, sum[j - 1] - sum[i] + suf[i] + pref[j]);
        }
    }
    
    return sum[numberOfInstances - 1] - res;
}

long long bestSubInterval() {
    int firstElement = instanceId * elementsPerInstance;
    int lastElement = min(n, (instanceId + 1) * elementsPerInstance) - 1;
    
    long long sum = 0;
    long long best = 0;
    
    for (int i = firstElement; i <= lastElement; i++) {
        sum += GetTaste(i);
        if (sum > 0) {
            sum = 0;
        }
        best = min(best, sum);
    }
    
    return best;
}

long long bestPrefix() {
    int firstElement = instanceId * elementsPerInstance;
    int lastElement = min(n, (instanceId + 1) * elementsPerInstance) - 1;
    
    long long sum = 0;
    long long best = 0;
    
    for (int i = firstElement; i <= lastElement; i++) {
        sum += GetTaste(i);
        best = min(best, sum);
    }
    
    return best;
}

long long bestSuffix() {
    int firstElement = instanceId * elementsPerInstance;
    int lastElement = min(n, (instanceId + 1) * elementsPerInstance) - 1;
    
    long long sum = 0;
    long long best = 0;
    
    for (int i = lastElement; i >= firstElement; i--) {
        sum += GetTaste(i);
        best = min(best, sum);
    }
    
    return best;
}

long long sumOfElements() {
    int firstElement = instanceId * elementsPerInstance;
    int lastElement = min(n, (instanceId + 1) * elementsPerInstance) - 1;
    
    long long sum = 0;
    
    for (int i = lastElement; i >= firstElement; i--) {
        sum += GetTaste(i);
    }
    
    return sum;
}

void sendResultToMainInstance(long long ans, long long bestPref, long long bestSuf, long long sum) {
    PutLL(0, ans);
    PutLL(0, bestPref);
    PutLL(0, bestSuf);
    PutLL(0, sum);
    Send(0);
}