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
//============================================================================
// Name        : PA2015/0A/Kanapka
// Author      : Kornel
// Description : Kanapka
//============================================================================

#include <stdio.h>
#include <stdio.h>
#include <algorithm>
#include "message.h"
#include "kanapka.h"

#define MASTER_ID 0
#define MAX_INSTANCE 100

using namespace std;

int nodes;
int nodeId;
long long N;

long long startIndex() {
	if (nodeId == 0) {
		return 0;
	}
	long long dataSize = N / nodes;
	int rest = N % nodes;
	return dataSize + rest + (nodeId - 1) * dataSize;
}

long long stopIndex() {
	long long dataSize = N / nodes;
	int rest = N % nodes;
	if (nodeId == 0) {
		return dataSize + rest -1;
	}
	return startIndex() + dataSize - 1;
}

int main() {

	N = GetN();
	nodes = NumberOfNodes() < N ? NumberOfNodes() : N;
	nodeId = MyNodeId();
	if (nodeId >= nodes) {
		return 0;
	}

	long long start = startIndex();
	long long stop = stopIndex();
	long long taste = 0;
	long long localTotal = 0;
	long long localMax = 0;
	long long localMin = 0;
	long long localMaxDiff = 0;

	for (long long inx = start; inx <= stop; inx++) {
		taste = GetTaste(inx);
		localTotal += GetTaste(inx);
		localMax = max(localMax, localTotal);
		localMin = min(localMin, localTotal);
		localMaxDiff = max(localMaxDiff, localMax - localTotal);
	}

	if (nodeId != MASTER_ID) {
		PutLL(MASTER_ID, localTotal);
		PutLL(MASTER_ID, localMax);
		PutLL(MASTER_ID, localMin);
		PutLL(MASTER_ID, localMaxDiff);
		Send(MASTER_ID);
	} else {
		long long globalTotal = localTotal;
		long long globalMax = localMax;
		long long globalMin = localMin;
		long long globalMaxDiff = localMaxDiff;
		for (int node = 1; node < nodes; node++) {
			Receive(node);
			localTotal = GetLL(node);
			localMax = GetLL(node);
			localMin = GetLL(node);
			localMaxDiff = GetLL(node);

			globalMaxDiff = max(globalMaxDiff, localMaxDiff);
			if (globalMax - (globalTotal + localMin) > globalMaxDiff) {
				globalMaxDiff = globalMax - (globalTotal + localMin);
			}
			if (globalTotal + localMax > globalMax) {
				globalMax = globalTotal + localMax;
			}
			globalTotal += localTotal;
		}
		printf("%lld\n", globalTotal + globalMaxDiff);
	}
	return 0;
}
/*
int main() {
    long long ID, NN, N, ST, SP, csum, cmin, cmax, cjmp, tsum, tmax, tjmp, t, i;

    ID = MyNodeId();
    NN = NumberOfNodes();

    N = GetN();
    ST = N * ID / NN;
    SP = N * (ID+1) / NN;

    csum = 0; cmin = 0; cmax = 0; cjmp = 0;
    for(i=ST; i<SP; ++i) {
        t = GetTaste(i);
        csum += t;
        if(csum < cmin) cmin = csum;
        if(csum > cmax) cmax = csum;
        if(cmax - csum > cjmp) cjmp = cmax - csum;
    }

    if(ID) {
        PutLL(0, csum);
        PutLL(0, cmin);
        PutLL(0, cmax);
        PutLL(0, cjmp);
        Send(0);
    } else {
        tsum = csum; tmax = cmax; tjmp = cjmp;
        for(i=1; i<NN; ++i) {
            Receive(i);
            csum = GetLL(i);
            cmin = GetLL(i);
            cmax = GetLL(i);
            cjmp = GetLL(i);

            if(cjmp > tjmp) tjmp = cjmp;
            if(tmax - (tsum + cmin) > tjmp) tjmp = tmax - (tsum + cmin);
            if(tsum + cmax > tmax) tmax = tsum + cmax;
            tsum += csum;
        }
        printf("%lld\n", tsum + tjmp);
    }
    return 0;
}
*/