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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <bits/stdc++.h>
#include "message.h"
#include "krazki.h"

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); }
template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr << sdbg << '=' << h << endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream& operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto vv : V) os << vv << ","; os << "]"; return os;
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const int SendAfter = 63990;

struct Sender {
    enum FieldType { FLD_CHAR, FLD_INT, FLD_LL };
    struct field {
        FieldType type;
        union { char ch; int n; LL l; };
    };
    struct Submitter {};

    vector<field> toSend;
    int dest;
    int cap;

    Sender() : dest(-10000), cap(SendAfter) {}
    Sender(int dst){
        dest = dst;
        cap = SendAfter;
        toSend.clear();
    }
    ~Sender(){
        if(toSend.size() > 0) submit();
    }

    int capacity() const { return cap; }
    void capacity(int newCapacity) { cap = newCapacity; }
    
    void submit(){
        LL msgsize = 4;
        if(toSend.size() == 0) return;
        PutInt(dest, (int)toSend.size());
        for(auto& f : toSend){
            switch(f.type){
                case FLD_CHAR: PutChar(dest, f.ch); msgsize++; break;
                case FLD_INT:  PutInt(dest, f.n); msgsize += 4; break;
                case FLD_LL:   PutLL(dest, f.l); msgsize += 8; break;
                default:       assert(false);
            }
        }
        Send(dest);
        toSend.clear();
    }

    void add_field(field f){
        assert(dest >= 0);
        toSend.push_back(f);
        if(toSend.size() >= cap) submit();
    }

    friend Sender& operator<<(Sender& snd, char ch){
        field f; f.type = FLD_CHAR; f.ch = ch; snd.add_field(f); return snd;
    }
    friend Sender& operator<<(Sender& snd, int n){
        field f; f.type = FLD_INT; f.n = n; snd.add_field(f); return snd;
    }
    friend Sender& operator<<(Sender& snd, LL l){
        field f; f.type = FLD_LL; f.l = l; snd.add_field(f); return snd;
    }
    friend Sender& operator<<(Sender& snd, Sender::Submitter){
        snd.submit(); return snd;
    }
};
Sender::Submitter submit;


struct Receiver {
    int numEnqueued;
    int node;

    Receiver() : node(-10000) {}
    Receiver(int nod) : node(nod), numEnqueued(0) {}

    void try_fetch(){
        assert(node >= -1);
        assert(numEnqueued >= 0);
        if(numEnqueued > 0){ numEnqueued--; return; }
        Receive(node);
        numEnqueued = GetInt(node);
        numEnqueued--;
    }

    char read_char(){ try_fetch(); return GetChar(node); }
    int read_int(){ try_fetch(); return GetInt(node); }
    LL read_ll(){ try_fetch(); return GetLL(node); }

    friend Receiver& operator>>(Receiver& recv, char& ch){
        ch = recv.read_char(); return recv;
    }
    friend Receiver& operator>>(Receiver& recv, int& n){
        n = recv.read_int(); return recv;
    }
    friend Receiver& operator>>(Receiver& recv, LL& l){
        l = recv.read_ll(); return recv;
    }
};

template<typename T>
Sender& operator<<(Sender& snd, const vector<T>& vec){
    snd << (int)vec.size();
    for(T v : vec) snd << v;
    return snd;
}

template<typename T>
Receiver& operator>>(Receiver& recv, vector<T>& vec){
    int sz;
    recv >> sz;
    vec = vector<T>(sz);
    for(int i = 0; i < sz; i++){
        recv >> vec[i];
    }
    return recv;
}



#define int LL

LL MyNode, TotalNodes;
vector<Sender> senders;
vector<Receiver> receivers;

void init(){
    MyNode     = MyNodeId();
    TotalNodes = NumberOfNodes();
    for(LL i = 0; i < TotalNodes; i++){
        senders.emplace_back(i);
        receivers.emplace_back(i);
    }
}


LL N, M;

// Wrappery.
int MyPipeHeight() {
  if (N == 0) {
    N = PipeHeight() + 1;
  }
  return N;
}

int MyNumberOfDiscs() {
  if (M == 0) {
    M = NumberOfDiscs();
  }
  return M;
}

long long MyHoleDiameter(long long i) {  // Od zera!
  return i == MyPipeHeight() - 1 ? 0 : HoleDiameter(i + 1);
}

long long MyDiscDiameter(long long i) {  // Od konca, od zera!
  return DiscDiameter(MyNumberOfDiscs() - i);
}


vector<LL> pipeStarts;
vector<LL> discStarts;
int S;

vector<LL> holeMinima, discMaxima;


LL runSequential(LL pipeFrom, LL pipeTo, LL discFrom, LL discTo) {
  vector<LL> radii(pipeTo - pipeFrom);
  for (LL i = pipeFrom; i < pipeTo; i++) {
    radii[i - pipeFrom] = MyHoleDiameter(i);
  }
  LL nPos = SZ(radii);
  for (LL i = 1; i < nPos; i++) {
    mini(radii[i], radii[i - 1]);
  }
  debug(radii, pipeFrom, pipeTo, discFrom, discTo);
  
  LL pipePtrStart = nPos - 1, pipePtr = pipePtrStart;
  for (LL i = discTo - 1; i >= discFrom; i--) {
    LL discRad = MyDiscDiameter(i);
    if (pipePtr < pipePtrStart) {
      pipePtr--;
    }
    while (pipePtr >= 0 && discRad > radii[pipePtr]) {
      pipePtr--;
    }
  }
  
  debug(pipePtr);
  
  return pipePtr + pipeFrom - discFrom + 1;
}


int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  init();
  
  MyPipeHeight();
  MyNumberOfDiscs();
  
  assert(TotalNodes % 2 == 0);
  S = TotalNodes / 2;
  for (int i = 0; i <= S; i++) {
    pipeStarts.push_back(N * i / S);
    discStarts.push_back(M * i / S);
  }
  debug(pipeStarts, discStarts, S, N, M);
  
  if (MyNode < S) {
    // Przetworz dziury.
    LL from = pipeStarts[MyNode],
       to   = pipeStarts[MyNode + 1];
    LL minDiam = numeric_limits<LL>::max();
    for (LL i = from; i < to; i++) {
      mini(minDiam, MyHoleDiameter(i));
    }
    debug(MyNode, minDiam);
    senders[0] << minDiam << submit;
    
  } else {
    // Przetworz krazki.
    LL from = discStarts[MyNode - S],
       to   = discStarts[MyNode - S + 1];
    LL maxDiam = -1;
    for (LL i = from; i < to; i++) {
      maxi(maxDiam, MyDiscDiameter(i));
    }
    debug(MyNode, maxDiam);
    senders[0] << maxDiam << submit;
  }
  
  
  // Master: przetworz info od nodow.
  if (!MyNode) {
    LL totalMin = numeric_limits<LL>::max();
    for (int i = 0; i < S; i++) {
      LL minDiam;
      receivers[i] >> minDiam;
      mini(totalMin, minDiam);
      holeMinima.push_back(totalMin);
    }
    
    discMaxima.resize(S);
    LL totalMax = -1;
    for (int i = S - 1; i >= 0; i--) {
      LL maxDiam;
      receivers[i + S] >> maxDiam;
      maxi(totalMax, maxDiam);
      discMaxima[i] = totalMax;
    }
    
    vector<int> blockStarts(S + 1, S - 1);
    
    for (int dBlock = 0; dBlock < S; dBlock++) {
      int fstHoleBlock = 0;
      while (fstHoleBlock < S && holeMinima[fstHoleBlock] >= discMaxima[dBlock]) {
        fstHoleBlock++;
      }
      assert(fstHoleBlock < S);
      blockStarts[dBlock] = fstHoleBlock;
    }
    debug(holeMinima, discMaxima, blockStarts);
    
    int nodeOrdered = 0;
    
    for (int dBlock = 0; dBlock < S; dBlock++) {
      for (int i = blockStarts[dBlock]; i <= blockStarts[dBlock + 1]; i++) {
        senders[nodeOrdered] << VI{dBlock, i} << submit;
        nodeOrdered++;
      }
    }
    
    for (int i = nodeOrdered; i < TotalNodes; i++) {
      senders[i] << VI{-1} << submit;
    }
  }
  debug("!");
  
  LL nodeRes = 1e18;
  
  // Kazdy node: odbierz info od mastera i odpal Krazki na odpowiednim przedziale.
  VI order;
  receivers[0] >> order;
  debug(MyNode, order);
  if (SZ(order) > 1) {
    int dBlock = order[0], hBlock = order[1];
    nodeRes = runSequential(pipeStarts[hBlock], pipeStarts[hBlock + 1],
                            discStarts[dBlock], discStarts[dBlock + 1]);
  }
  senders[0] << nodeRes << submit;
  
  // Master: odbierz wyniki i wypisz minimum zmaksowane z 0
  if (!MyNode) {
    LL totalRes = 1e18;
    for (int i = 0; i < TotalNodes; i++) {
      LL res;
      receivers[i] >> res;
      mini(totalRes, res);
    }
    cout << max(0LL, totalRes) << "\n";
  }
}