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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "kanapka.h"
//uncomment when testing remotely
#include "message.h"
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    long long numberOfSandwichParts = GetN(), numberOfPartsPerNode, myNumberOfParts, side1Max, side1Index, myMaxValue, myMaxIndex, myStartPartIndex, resultForMyPart;
    long long* myParts, *resultsFromNodes, *maxInNodes, *indexOfMaxInNodes, *myMax, *side1MaxPointer;
//    int myNodeNumber = 0;
//    int numberOfNodes = 1;
    int myNodeNumber = MyNodeId(), numberOfNodes = NumberOfNodes();
    
    numberOfPartsPerNode = numberOfSandwichParts / numberOfNodes;
//    round to higher
    if (numberOfSandwichParts % numberOfNodes != 0)
    {
        numberOfPartsPerNode += 1;
    }
    
    myStartPartIndex = myNodeNumber * numberOfPartsPerNode;
    numberOfPartsPerNode = numberOfSandwichParts / numberOfNodes;
    myNumberOfParts = numberOfPartsPerNode;
//    last node might have a bit less parts to calculate then rest
    if (myNodeNumber == (numberOfNodes - 1)) {
        myNumberOfParts = numberOfSandwichParts - myStartPartIndex;
    }
    
    myParts = new long long[myNumberOfParts];
    
    myParts[0] = GetTaste(myStartPartIndex);
    
    for (long long i=1; i < myNumberOfParts; i++) {
        myParts[i] = myParts[i-1] + GetTaste(i);
    }
    resultForMyPart = myParts[myNumberOfParts - 1];
    
//    create an array for keeping results from all previous nodes
    resultsFromNodes = new long long[numberOfNodes];

//    wait for each previous as it send its data
//    load this data to an array
    for (int i = 0; i < myNodeNumber; i++) {
        Receive(i);
        resultsFromNodes[i] = GetLL(i);
    }
    
//    send your result in loop to all next nodes
    for (int i = myNodeNumber+1; i < numberOfNodes - 1; i++) {
        PutLL(i, resultForMyPart);
        Send(i);
    }
    
//    recalculate your results in myParts array
    for (long long i=0; i < myNumberOfParts; i++) {
        for (int j = 0; j < myNodeNumber; j++) {
            myParts[i] = myParts[i] + resultsFromNodes[j];
        }
    }
    
//    find my maxValue
    myMax = max_element(myParts, myParts + myNumberOfParts);
    myMaxValue = *myMax;
    myMaxIndex = myStartPartIndex + distance(myParts, myMax);
    
    
//    create an array to store results from all nodes
    maxInNodes = new long long[numberOfNodes];
    indexOfMaxInNodes = new long long[numberOfNodes];
//    node 0 finds max from all nodes
    if (myNodeNumber > 0) {
        PutLL(0, myMaxIndex);
        Send(0);
    } else {  // MyNodeId == 0
        maxInNodes[0] = myMaxValue;
        indexOfMaxInNodes[0] = myMaxIndex;
        for (int nodeId = 1; nodeId < numberOfNodes; ++nodeId) {
            Receive(nodeId);
            indexOfMaxInNodes[nodeId] = GetLL(nodeId);
            maxInNodes[nodeId] = GetTaste(indexOfMaxInNodes[nodeId]);
        }
        
//        find max for side 1
        side1MaxPointer = max_element(maxInNodes, maxInNodes+numberOfNodes);
        side1Max = *side1MaxPointer;
        side1Index = indexOfMaxInNodes[distance(maxInNodes, side1MaxPointer)];
    }
    
//    recalculate everything for side 2
//    recalculate everything for side 2
//    recalculate everything for side 2

    numberOfPartsPerNode = numberOfSandwichParts / numberOfNodes;
    //    round to higher
    if (numberOfSandwichParts % numberOfNodes != 0)
    {
        numberOfPartsPerNode += 1;
    }
    
    myStartPartIndex = myNodeNumber * numberOfPartsPerNode;
    numberOfPartsPerNode = numberOfSandwichParts / numberOfNodes;
    myNumberOfParts = numberOfPartsPerNode;
    //    last node might have a bit less parts to calculate then rest
    if (myNodeNumber == (numberOfNodes - 1)) {
        myNumberOfParts = numberOfSandwichParts - myStartPartIndex;
    }
    
    myParts = new long long[myNumberOfParts];
    
    myParts[0] = GetTaste(myStartPartIndex);
    
    for (long long i=1; i < myNumberOfParts; i++) {
        myParts[i] = myParts[i-1] + GetTaste(i);
    }
    resultForMyPart = myParts[myNumberOfParts - 1];
    
    //    create an array for keeping results from all previous nodes
    resultsFromNodes = new long long[numberOfNodes];
    
    //    wait for each previous as it send its data
    //    load this data to an array
    for (int i = 0; i < myNodeNumber; i++) {
        Receive(i);
        resultsFromNodes[i] = GetLL(i);
    }
    
    //    send your result in loop to all next nodes
    for (int i = myNodeNumber+1; i < numberOfNodes - 1; i++) {
        PutLL(i, resultForMyPart);
        Send(i);
    }
    
    //    recalculate your results in myParts array
    for (long long i=0; i < myNumberOfParts; i++) {
        for (int j = 0; j < myNodeNumber; j++) {
            myParts[i] = myParts[i] + resultsFromNodes[j];
        }
    }
    
    //    find my maxValue
    myMax = max_element(myParts, myParts + myNumberOfParts);
    myMaxValue = *myMax;
    myMaxIndex = myStartPartIndex + distance(myParts, myMax);
    
    
    //    create an array to store results from all nodes
    maxInNodes = new long long[numberOfNodes];
    indexOfMaxInNodes = new long long[numberOfNodes];
    //    node 0 finds max from all nodes
    if (myNodeNumber > 0) {
        PutLL(0, myMaxIndex);
        Send(0);
    } else {  // MyNodeId == 0
        maxInNodes[0] = myMaxValue;
        indexOfMaxInNodes[0] = myMaxIndex;
        for (int nodeId = 1; nodeId < numberOfNodes; ++nodeId) {
            Receive(nodeId);
            indexOfMaxInNodes[nodeId] = GetLL(nodeId);
            maxInNodes[nodeId] = GetTaste(indexOfMaxInNodes[nodeId]);
        }
        
        //        find max for side 1
        side1MaxPointer = max_element(maxInNodes, maxInNodes+numberOfNodes);
        side1Max = *side1MaxPointer;
        side1Index = indexOfMaxInNodes[distance(maxInNodes, side1MaxPointer)];

    cout << 14 << endl;
    }
    
    
    
//    compare index of max value from side1 and from side2
    
//    find almostMax1 for side1 in scope from 0 to index of side2max
    
//    find almostMax2 for side2 in scope from 0 to index of side1max
    
//    choose max from almostMax1 and almostMax2
    
//    add almostMax to opposite max
    return 0;
}