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

using namespace std;


#define log if (false) cerr
typedef long long ll;
typedef unsigned long long ull;

const ll PARALLEL_TRIGGER = 10000000;


struct Tastes
{
    ll maxLeft;
    ll maxRight;
    ll maxBoth;
    ll total;

    Tastes() : maxLeft(0), maxRight(0), maxBoth(0), total(0)
    {}
};

struct LocalMax
{
    int index;
    ll taste;
};


Tastes computeTastes(int fromN, int toN)
{
    Tastes tastes;
    ll total = 0;

    for (int i = fromN; i < toN; i++)
    {
        total += GetTaste(i);
    }

    ll maxLeft = 0;
    ll maxRight = max(0LL, total);
    ll maxBoth = maxRight;
    ll sumLeft = 0;
    ll sumRight = total;

    for (int i = fromN; i < toN; i++)
    {
        ll taste = GetTaste(i);
        sumLeft += taste;
        sumRight -= taste;
        maxLeft = max(maxLeft, sumLeft);
        maxRight = max(maxRight, sumRight);
        maxBoth = max(maxBoth, maxLeft + sumRight);

        log << sumLeft << ", " << sumRight << " - " << maxLeft << ", " << maxRight;
        log << ", " << maxBoth << endl;
    }

    tastes.maxLeft = maxLeft;
    tastes.maxRight = maxRight;
    tastes.maxBoth = maxBoth;
    tastes.total = total;

    return tastes;
}

void sendTastes(const Tastes& tastes)
{
    PutLL(0, tastes.maxLeft);
    PutLL(0, tastes.maxRight);
    PutLL(0, tastes.maxBoth);
    PutLL(0, tastes.total);
    Send(0);
}

Tastes receiveTastes(ll nodeId)
{
    Tastes tastes;
    Receive(nodeId);
    tastes.maxLeft = GetLL(nodeId);
    tastes.maxRight = GetLL(nodeId);
    tastes.maxBoth = GetLL(nodeId);
    tastes.total = GetLL(nodeId);
    return tastes;
}

ll mergeTastes(ll numberOfNodes)
{
    Tastes result;
    vector<Tastes> allTastes;

    for (ll i = 0; i < numberOfNodes; i++)
    {
        allTastes.push_back(receiveTastes(i));
    }

    ll total = 0;
    for (Tastes tastes : allTastes)
    {
        total += tastes.total;
    }

    ll maxLeft = 0;
    ll maxRight = max(0LL, total);
    ll maxBoth = maxRight;
    ll sumLeft = 0;
    ll sumRight = total;

    for (int i = 0; i < allTastes.size(); i++)
    {
        Tastes tastes = allTastes[i];

        maxBoth = max(maxBoth, maxLeft + sumRight-tastes.total + tastes.maxRight);

        maxLeft = max(maxLeft, sumLeft + tastes.maxLeft);

        sumLeft += tastes.total;
        sumRight -= tastes.total;

        maxRight = max(maxRight, sumRight + tastes.maxRight);

        maxBoth = max(maxBoth, sumLeft-tastes.total + sumRight + tastes.maxBoth);

        log << sumLeft << ", " << sumRight << " - " << maxLeft << ", " << maxRight;
        log << ", " << maxBoth << endl;
    }


    return maxBoth;
}


int _main()
{
    if (MyNodeId() == 0)
    {
        ll n = GetN();
        Tastes tastes = computeTastes(0, n);
        cout << tastes.maxBoth;
    }
    return 0;
}

int main()
{
    ll n = GetN();
    if (n < PARALLEL_TRIGGER)
    {
        _main();
        return 0;
    }
    ll myNodeId = MyNodeId();
    ll numberOfNodes = NumberOfNodes();

    ll from = myNodeId * n / numberOfNodes;
    ll to = (myNodeId + 1) * n / numberOfNodes;

    Tastes tastes = computeTastes(from, to);
    sendTastes(tastes);

    if (myNodeId == 0)
    {
        ll result = mergeTastes(numberOfNodes);
        cout << result;
    }

    return 0;
}