Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8. Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
  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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// Artur Kraska, II UWr

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>

#include "message.h"
#include "skup.h"


#define forr(i, n)                  for(int i=0; i<(int)n; i++)
#define FOREACH(iter, coll)         for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,R,PRED)            ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})
#define testy()                     int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=(int)a; i<=(int)b; i++)
#define FORD(i, a, b)               for(int i=(int)a; i>=(int0b; i--)
#define MP                          make_pair
#define PB                          push_back
#define deb(X)                      X;

#define M 1000000007
#define INF 1000000007

// oznacza ile milisekund bedzie czekac z wyslaniem
#define TIME2 15
// oznacza jakiego czasu nie moze przekroczyc
#define TIME1 10

// CZESC DO TESTOW

#define TESTUJ true
#define cout if(0) cout

bool initialized = 0;

char slowo[107][107];
/*
void initialize()
{
    initialized = 1;
    int x = NumberOfNodes();
    forr(i, x)
        scanf("%s", slowo[i]);
}

int GetInt2(int nr)
{
    if(!initialized)
    {
        initialize();
    }
    int y = GetInt(nr);
    return slowo[MyNodeId()][nr] == '1' ? y : 0;
}

#define GetInt GetInt2
*/
/*

long long NumberOfCompanies() {
  return 1000;
}

long long GetShareCost(long long Id) {
  assert(0 <= Id && Id < 1000);
  return 1000 * 1000 * 1000;
}
*/

// KONIEC TESTOWANIA

using namespace std;

int ILE, NR;
int in[1007], out[1007], in2[1007], out2;
vector <int> kolejnosc;
int min_na_cyklu;

void generuj_kolejnosc_komunikacji()
{
    if(NR != 0 && NR != ILE/2)
    {
        kolejnosc.PB(ILE-NR);
    }
    FOR(i, 1, ILE)
    {
        if(2*NR%ILE != i)
            kolejnosc.PB((i-NR+ILE)%ILE);
    }
/*
    forr(i, ILE-1)
    {
        cout << "kolejnosc: " << kolejnosc[i] << endl;
    }*/
}

void znajdz_wchodzace()
{
    forr(i, ILE)
        if(i != NR)
        {
            PutInt(i, 1);
            Send(i);
        }

    forr(i, ILE)
        if(i != NR)
        {
            Receive(i);
            in[i] = GetInt(i);
            if(in[i])
                cout << " jest krawedz in z " << i << " do " << NR << endl;
        }
}

void powiedz(int nr, bool b)
{
    if(TESTUJ)
    {
        PutLL(nr, b);
        Send(nr);
        return ;
    }

    Receive(nr);
    Send(nr);
    if(b)
    {
        long long start = clock();
        while(clock() - start < TIME2);
    }
    Send(nr);
}

bool zapytaj(int nr)
{
    if(TESTUJ)
    {
        Receive(nr);
        return GetLL(nr);
    }

    Send(nr);
    Receive(nr);
    long long start = clock();
    Receive(nr);
    return clock()-start >= TIME1;
}

void synchronizuj(int i)
{
    if(NR == i)
    {
        cout << "   teraz ja synchronizuje" << endl;
        FOR(j, 0, ILE-1)
            if(j != NR)
            {
                Receive(j);
                cout << "\t\t\t\t\t\t" << " dostalem od " << j << endl;
                GetInt(j);
                GetInt(j);
                GetInt(j);
            }
        FOR(j, 0, ILE-1)
            if(j != NR)
            {
                PutInt(j, 0);
                PutInt(j, 0);
                PutInt(j, 0);
                Send(j);
                cout << "\t\t\t\t\t\t" << " wysylam do " << j << endl;
            }
        cout << "zsynchronizowane" << endl;
    }
    else
    {
        PutInt(i, 0);
        PutInt(i, 0);
        PutInt(i, 0);
        Send(i);
        cout << "\t\t\t\twyslalem do " << i << endl;
        Receive(i);
        cout << "\t\t\t\t odebralem od " << i << endl;
        GetInt(i);
        GetInt(i);
        GetInt(i);
    }
}

void wait()
{
//    long long t = clock();
//    while(clock()-t < 100);
}

void przeslij_wychodzace_za_pomoca(int pom, int pom2)
{
    cout << "wszedlem do przesylania za pomoca " << pom << endl;
    int nr;

    if(pom != NR && NR != pom2) // synchronizowany
    {
        int ile = 0;
        //Send(pom);
        //Send(pom);
        //Receive(pom);
        FOR(i, 0, ILE-1)
            if(i != pom && in[i] && i != pom2)
            {
                ile++;
                PutInt(i, 0);
                Send(i);
            }
        //Send(pom);
        wait();
        if(ile == 0)
        {
            Send(pom);
        }
        while((nr = Receive(-1)) != pom)
        {
//            cout << " mamy krawedz out z " << NR << " do " << nr << endl;
            int x = GetInt(nr);
            if(x == 0)
            {
                out[nr] = 1;
                PutInt(nr, 1);
                Send(nr);
            }
            else // x == 1
            {
                ile--;
                if(ile == 0)
                {
                    Send(pom);
                }
            }
        }
/*        Send(pom);
        wait();
        while((nr = Receive(-1)) != pom)
        {
            cout << " mamy krawedz out z " << NR << " do " << nr << endl;
            out[nr] = 1;
        }*/
    }
    else if(NR == pom) // pierwszy pomocniczy
    {/*
        forr(j, ILE)
        {
            if(j != pom && j != pom2)
                Receive(j);
        }
        forr(j, ILE)
        {
            if(j != pom && j != pom2)
                Send(j);
        }*/
        forr(j, ILE)
        {
            if(j != pom && j != pom2)
                Receive(j);
        }
        forr(j, ILE)
        {
            if(j != pom && j != pom2)
                Send(j);
        }
        /*
        wait();

        forr(j, ILE)
        {
            if(j != pom && j != pom2)
                Send(j);
        }


        forr(j, ILE)
        {
            if(j != pom && j != pom2)
                Receive(j);
        }

        wait();

        forr(j, ILE)
        {
            if(j != pom && j != pom2)
                Send(j);
        }*/
    }

    cout << " wyszedlem od " << pom << endl;
}

void znajdz_wychodzace()
{
    //int pom = 0, nr;//, pom2 = 1, pom3 = 2;

    synchronizuj(5);
    przeslij_wychodzace_za_pomoca(0, 6);

    synchronizuj(6);
    przeslij_wychodzace_za_pomoca(1, 7);

    synchronizuj(7);
    przeslij_wychodzace_za_pomoca(2, 8);

    synchronizuj(8);

    return ;

    // teraz wszystko z i do 0, pomijaj�c krawedz do pom1
//    synchronizuj(6);
/*
    if(pom == NR) // synchronizowany
    {
        Send(pom2);
        FOR(i, 0, ILE-1)
            if(i != pom && i != pom2 && in[i])
            {
                Send(i);
            }
        Send(pom2);
        while((nr = Receive(-1)) != pom2)
        {
            cout << " mamy krawedz out z " << NR << " do " << nr << endl;
            out[nr] = 1;
        }
    }
    else  if(NR != pom && NR != pom2) // pierwszy pomocniczy
    {
        Send(pom2);
        if(in[pom])
            Send(pom);
        Send(pom2);
        Receive(pom2);
    }
    else // NR == pom2
    {
        forr(j, ILE)
        {
            if(j != pom2)
                Receive(j);
        }
        forr(j, ILE)
        {
            if(j != pom2)
                Receive(j);
        }

        wait();

        forr(j, ILE)
        {
            if(j != pom2)
                Send(j);
        }
    }
    synchronizuj(7);
*//*
    // teraz krawedzie pom-pom2
    if(NR == pom || NR == pom2)
    {
        int drugi = (NR == pom ? pom2 : pom);
        Send(pom3);
        if(in[drugi])
            Send(drugi);
        Send(pom3);
        while((nr = Receive(-1)) != pom3)
        {
            cout << " mamy krawedz out z " << NR << " do " << nr << endl;
            out[nr] = 1;
        }
    }
    else if(NR == pom3)
    {
        Receive(pom);
        Receive(pom2);
        Receive(pom);
        Receive(pom2);
        wait();
        Send(pom2);
        Send(pom);
    }
*/
//    synchronizuj(4);

/*
    forr(i, ILE)
    {
        synchronizuj(i);

        int pom = (i+1)%ILE, pom2 = (i+2)%ILE, nr;
        if(i == NR) // synchronizowany
        {
            Send(pom);
            while((nr = Receive(-1)) != pom)
            {
                cout << " mamy krawedz out z " << NR << " do " << nr << endl;
                out[nr] = 1;
            }
            Send(pom2);
            while((nr = Receive(-1)) != pom2)
            {
                cout << " mamy krawedz out z " << NR << " do " << nr << endl;
                out[nr] = 1;
            }
        }
        else if(NR == pom) // pierwszy pomocniczy
        {
            Receive(i);
            forr(j, ILE)
            {
                if(j != i && j != pom)
                    Receive(j);
            }
            Send(i);

            if(in[i])
                Send(i);
            Send(pom2);
        }
        else if(NR == pom2) // drugi pomocniczy
        {
            if(in[i])
            {
                Send(i);
            }
            Send(pom);

            Receive(i);
            Receive(pom);
            Send(i);
        }
        else  // pozostale
        {
            if(in[i])
            {
                Send(i);
            }
            Send(pom);
        }
    }
    synchronizuj(4);

*/
/*    forr(i, ILE-1)
    {
        int nr = kolejnosc[i];
        if(NR < nr)
        {
            powiedz(nr, in[nr]);
            out[nr] = zapytaj(nr);
        }
        else
        {
            out[nr] = zapytaj(nr);
            powiedz(nr, in[nr]);
        }
        if(out[nr])
        {
            cout << "mam krawedz do " << nr << " :)" << endl;
        }
        else
        {
//            cout << "nie mam krawedzi do " << nr << " :(" << endl;
        }
    }*/
}

void wybierz_najmniejsza_wychodzaca()
{
    out2 = -1;
    forr(i, ILE)
        if(out[i])
        {
            out2 = i;
            cout << " out2 z " << NR << " to " << out2 << endl;
            return ;
        }
}

void filtruj_najmniejsze_wchodzace()
{
    cout << " filtruje" << endl;
    forr(i, ILE)
    {
        if(i != NR)
        {
            PutInt(i, out2+1);
            cout << "    wysylam " << out2+1 << " do " << i << endl;
            Send(i);
        }
    }

    forr(i, ILE)
    {
        if(i != NR)
        {
            cout << "      odbieram od " << i << endl;
            Receive(i);
            int x = GetInt(i);
            if(x == NR+1)
            {
                cout << "   mam in2 do " << i << endl;
                in2[i] = 1;
            }
//            cout << "     " << i << " przyslal mi " << x << ", czyli in2[" << i << "] = " << in2[i] << endl;
        }
    }
    cout << " przefiltrowalem" << endl;
    /*
    forr(i, ILE-1)
    {
        int nr = kolejnosc[i];
        if(NR < nr)
        {
            powiedz(nr, nr == out2);
            in2[nr] = zapytaj(nr);
            cout << "\t\t\t\t\tmowie do " << nr << ", ze " << (nr == out2) << endl;
            cout << "\t\t\t\t\t" << nr << " powiedzial mi, ze " << in2[nr] << endl;
        }
        else
        {
            in2[nr] = zapytaj(nr);
            powiedz(nr, nr == out2);
            cout << "\t\t\t\t\t" << nr << " powiedzial mi, ze " << in2[nr] << endl;
            cout << "\t\t\t\t\tmowie do " << nr << ", ze " << (nr == out2) << endl;
        }
        if(in2[nr])
        {
            cout << "mam krawedz wchodzaca2 z " << nr << " :)" << endl;
        }
        else
        {
//            cout << "nie mam krawedzi wchodzacej2 z " << nr << " :(" << endl;
        }
    }*/
}

int znajdz_min_na_cyklu()
{
    cout << "  szukam min na cyklu" << endl;
    int minn = NR, b;
    forr(c, ILE+2)
    {
        if(out2 >= 0)
        {
            PutInt(out2, minn);
            Send(out2);
        }
        minn = INF;
        forr(i, ILE)
            if(in2[i])
            {
                Receive(i);
                b = GetInt(i);
                minn = min(minn, b);
            }
        if(minn < INF)
            minn = min(minn, NR);
    }
    return minn;
}

void przeslij_i_wypisz()
{
//    cout << "\t\t zaczynam przesylac wynik" << endl;
    cout << "  zaczynam przesylanie " << endl;
    int n;
    vector <int> res;
    n = NumberOfCompanies();
    forr(i, n)
        res.PB(-GetShareCost(i));

    forr(i, ILE)
        if(in2[i] && i != min_na_cyklu)
        {
//            cout << " odbieram od " << i << endl;
            Receive(i);
            n = GetInt(i);
//            cout << "n: " << n << endl;
            forr(j, n)
                res.PB(GetInt(i));
        }

    cout << "min na cyklu: " << min_na_cyklu << ", out2: " << out2 << endl;
    if(NR != min_na_cyklu && out2 != -1)
    {
        PutInt(out2, res.size());
        forr(i, res.size())
            PutInt(out2, res[i]);
        Send(out2);
    }
    else
    {
        cout << "wynik ma " << res.size() << " elementow" << endl;
        sort(res.begin(), res.end());
        long long result = 0;
        forr(i, res.size())
        {
            result += -(i+1)*(long long)res[i];
        }
        printf("%lld\n", result);
    }
}

int main()
{
    ILE = NumberOfNodes();
    NR = MyNodeId();

    znajdz_wchodzace();
    generuj_kolejnosc_komunikacji();
    znajdz_wychodzace();
    wybierz_najmniejsza_wychodzaca();
    filtruj_najmniejsze_wchodzace();
    min_na_cyklu = znajdz_min_na_cyklu();
    przeslij_i_wypisz();

    return 0;
}