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

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

#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAINS(A,X) (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U> &_p) { return os << "(" << _p.X << "," << _p.Y << ")"; }
template<typename T> ostream& operator << (ostream &os, const vector<T>& _V) { bool f = true; os << "["; for(auto v: _V) { os << (f ? "" : ",") << v; f = false; } return os << "]"; }
template<typename T> ostream& operator << (ostream &os, const set<T>& _S) { bool f = true; os << "("; for(auto s: _S) { os << (f ? "" : ",") << s; f = false; } return os << ")"; }
template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& _M) { return os << set<pair<T, U>>(ALL(_M)); }
typedef signed long long slong;
typedef long double ldouble;
typedef pair<int,int> pii;
const slong INF = 1000000100;
const ldouble EPS = 1e-9;

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

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

typedef vector<int> vi;

int PROC, ID;
const int MASTER = 0;

int get_int(int source) {
    Receive(source);
    return GetInt(source);
}

void send_int(int target, int x) {
    PutInt(target, x);
    Send(target);
}

slong get_slong(int source) {
    Receive(source);
    return GetLL(source);
}

void send_slong(int target, slong x) {
    PutLL(target, x);
    Send(target);
}

const int MAXN = 1000;

struct info {
    int real;
    int id;
    vi in_edges;
    vi prices;

    info() { /// puste info
        real = 0;
        id = 0;
        in_edges = vi(PROC,0);
        prices = vi(MAXN,0);
    }

    info(vi _in_edges) { /// lokalne info
        real = 1;
        id = ID;
        in_edges = _in_edges;
        int n = NumberOfCompanies();
        FORW(i,0,n) {
            prices.PB(GetShareCost(i));
        }
        FORW(i,n,MAXN) { /// dlugosc wiadomosci powinna byc stala
            prices.PB(0);
        }
    }

    info(int source) { /// odbierz info
        Receive(source);
        real = GetInt(source);
        id = GetInt(source);
        FORW(i,0,PROC) {
            in_edges.PB(GetInt(source));
        }
        FORW(i,0,MAXN) {
            prices.PB(GetInt(source));
        }
    }

    void send_info(int target) {
        /// TODO: napisac
        PutInt(target, real);
        PutInt(target, id);
        for(int e : in_edges) {
            PutInt(target, e);
        }
        for(int p : prices) {
            PutInt(target, p);
        }
        Send(target);
    }
};

void solve()
{
    /// wyslij wszystkim 1, wyznacz krawedzie wchodzace
    FORW(id,0,PROC) {
        send_int(id, 1);
    }
    vi in_edges(PROC, 0);
    FORW(id,0,PROC) {
        int x = get_int(id);
        in_edges[id] = x;
    }

    vector<info> M(PROC);
    vector<vi> sent(PROC, vi(PROC,0));
    /// sent[i][j] - czy wyslalismy do i info o j
    M[ID] = info(in_edges);
    //debug(M[ID].in_edges);

    int ROUNDS = PROC + 3; /// TODO: wystarczy???
    if(PROC <= 20) {
        ROUNDS = 2 * PROC + 3;
    }
    if(PROC > 35) {
        ROUNDS = 100 / PROC;
    }

    vi others;
    FORW(i,0,PROC) {
        if(i != ID) others.PB(i);
    }
    FORW(it,0,ROUNDS) {
        /// kazdemu sprobuj wyslac cos nowego
        for(int i : others) {
            info m;
            FORW(j,0,PROC) {
                if(M[j].real && !sent[i][j]) {
                    m = M[j];
                    sent[i][j] = 1;
                    break;
                }
            }
            //if(m.real) debug(i, m.id, "wyslij");
            m.send_info(i);
        }

        /// odbierz wiadomosci
        for(int i : others) {
            info m(i);
            if(m.real && !M[m.id].real) {
                //debug(i, m.id, "odbierz");
                M[m.id] = m;
            }
        }
    }

    /// TODO: sprawdz czy jestem najmniejszym procesem
    /// ktory ma pelne info, jak tak to policz wynik

    int cnt = 0;
    FORW(i,0,PROC) {
        cnt += M[i].real;
    }
    if(cnt != PROC) return;

    //cerr << "mam wszystko!\n";

    vector<vi> G(PROC); /// graf odwroconych krawedzi
    FORW(i,0,PROC) {
        FORW(j,0,PROC) {
            if(M[i].in_edges[j]) {
                G[i].PB(j);
            }
        }
    }

    vi debesciaki;
    FORW(id,0,PROC) {
        vi vis(PROC,0);
        queue<int> q;
        cnt = 1;
        vis[id] = 1;
        q.push(id);
        while(q.size()) {
            int x = q.front();
            q.pop();
            for(int y : G[x]) {
                if(!vis[y]) {
                    vis[y] = 1;
                    cnt++;
                    q.push(y);
                }
            }
        }
        if(cnt == PROC) {
            debesciaki.PB(id);
        }
    }
    //debug(debesciak);
    srand(4242);
    int debesciak = debesciaki[rand() % SIZE(debesciaki)];
    if(ID != debesciak) return;

    vector<slong> prices;
    FORW(id,0,PROC) {
        for(int p : M[id].prices) {
            if(p > 0) {
                prices.PB(p);
            }
        }
    }

    //debug(prices);

    sort(ALL(prices));
    reverse(ALL(prices));
    slong ans = 0;
    cnt = 1;
    for(slong p : prices) {
        ans += p * cnt;
        cnt++;
    }

    cout << ans << "\n";
}

int main()
{
    PROC = NumberOfNodes();
    ID = MyNodeId();

    solve();

    return 0;
}