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

using namespace std;

int myInst, inst;
int n;

int main()
{
    n = GetN();
    inst = NumberOfNodes();
    myInst = MyNodeId();

    if(myInst > n - 1)
        return 0;
    if(inst > n)
        inst = n;

    int remainder = n % inst;

    int myRangeMin, myRangeMax;

    myRangeMin = myInst * floor(n / inst) + min(myInst, remainder);
    myRangeMax = myRangeMin + floor(n / inst);

    if(myInst < remainder)
        ++myRangeMax;

    long long sum = 0;
    long long maxx = 0;

    for(int i = myRangeMin; i < myRangeMax; ++i)
    {
        sum += GetTaste(i);
        maxx = max(maxx, sum);
    }

    int borderInst = 0;

    int wyn = 0;

    // Wyslij/zbierz

    if(myInst != 0)
    {
        PutLL(0, sum);
        PutLL(0, maxx);
        Send(0);
    }
    else
    {
        for(int i = 1; i < inst; ++i)
        {
            int nsum, nmaxx, nborder;
            Receive(i);
            nsum = GetLL(i);
            nmaxx = GetLL(i);

            if(sum + nmaxx > maxx)
            {
                maxx = sum + nmaxx;
                borderInst = i;
            }

            if(sum + nsum > maxx)
            {
                maxx = sum + nsum;
                borderInst = i;
            }

            sum += nsum;
        }

        wyn = maxx;
    }

    // ===

    cout << "pierwsze przejscie";

    sum = 0;
    maxx = 0;

    for(int i = myRangeMax - 1; i >= myRangeMin; ++i)
    {
        sum += GetTaste(i);
        maxx = max(maxx, sum);
    }

    // Wyslij/zbierz

    if(myInst != n - 1)
    {
        PutLL(n - 1, sum);
        PutLL(n - 1, maxx);
        Send(n - 1);
    }
    else
    {
        for(int i = n - 2; i >= borderInst; ++i)
        {
            int nsum, nmaxx;

            Receive(i);
            nsum = GetLL(i);
            nmaxx = GetLL(i);

            maxx = max(maxx, sum + nmaxx);
            maxx = max(maxx, sum + nsum);

            sum += nsum;
        }

        PutLL(0, maxx);
        Send(0);
    }

    // ===

    cout << "drugie przejscie" << endl;

    if(myInst == 0)
    {
        Receive(n - 1);
        wyn += GetLL(n - 1);
        printf("%lld", wyn);
    }

    return 0;
}