#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
const int MAXN = 2e5;
const int MXVAL = 1e9;
int n;
int ratings[MAXN];
int indeg[MAXN];
vector<int> in_edges[MAXN];
int out_val[MAXN];
int costs[MAXN];
bool vis[MAXN];
ll ans = 0;
class slopeset {
public:
map<int, ll> slopes; // positive values
ll init_val = 0;
slopeset(ll ival = 0) {
init_val = ival;
}
void merge(slopeset *other) {
init_val += other->init_val;
for (auto it: other->slopes) {
slopes[it.first] += it.second;
}
delete other;
}
void insert(int p, ll c_diff) { // it is c_diff cheaper at point p
ll r = c_diff;
auto nval = slopes.upper_bound(p);
while (nval != slopes.end() && nval->second <= r) {
r -= nval->second;
slopes.erase(nval);
nval = slopes.upper_bound(p);
}
if (nval != slopes.end()) slopes[nval->first] -= r;
slopes[p] += c_diff;
}
int size() {
return slopes.size();
}
};
slopeset *ssets[MAXN];
void prune(int cur) {
vis[cur] = 1;
ssets[cur] = new slopeset(costs[cur]);
for (int nxt: in_edges[cur]) {
if (ssets[nxt]->size() > ssets[cur]->size()) {
ssets[nxt]->merge(ssets[cur]);
ssets[cur] = ssets[nxt];
}
else ssets[cur]->merge(ssets[nxt]);
}
ssets[cur]->insert(ratings[cur], costs[cur]);
int nxt = out_val[cur];
indeg[nxt]--;
if (!indeg[nxt]) prune(nxt);
}
slopeset *sset;
void collect_vals(int cur, map<int, ll> &saved) {
vis[cur] = 1;
for (int nxt: in_edges[cur]) {
if (indeg[nxt]) continue;
if (ssets[nxt]->size() > sset->size()) {
ssets[nxt]->merge(sset);
sset = ssets[nxt];
}
else sset->merge(ssets[nxt]);
}
sset->init_val += costs[cur];
saved[ratings[cur]] += costs[cur];
if (!vis[out_val[cur]]) collect_vals(out_val[cur], saved);
}
void add_to(int cur) {
map<int, ll> saved; // amount saved by choosing this value
sset = new slopeset();
collect_vals(cur, saved);
if (saved.find(MXVAL) == saved.end()) saved[MXVAL] = 0;
ll cur_costs = sset->init_val;
auto spos = sset->slopes.begin();
ll best = -1;
for (auto it: saved) {
while (spos != sset->slopes.end() && spos->first <= it.first) {
cur_costs -= spos->second;
spos++;
}
if (best == -1 || cur_costs-it.second < best) best = cur_costs-it.second;
}
ans += best;
}
ll bash(vector<int> &vals, ll c = 0) {
if (vals.size() == n) {
for (int i = 0; i < n; i++) {
if (vals[out_val[i]] > vals[i]) return -1;
}
return c;
}
ll best = -1;
int p = vals.size();
for (int i = 1; i <= 10; i++) {
vals.push_back(i);
if (i != MXVAL+1-ratings[p]) c += costs[p];
ll cval = bash(vals, c);
if (best == -1 || (cval != -1 && cval < best)) best = cval;
if (i != MXVAL+1-ratings[p]) c -= costs[p];
vals.pop_back();
}
return best;
}
ll solve() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
//ifstream fin("report.in");
cin >> n;
ans = 0;
for (int i = 0; i < n; i++) {
indeg[i] = 0;
in_edges[i].clear();
vis[i] = 0;
}
for (int i = 0; i < n; i++) {
int a;
cin >> a >> ratings[i] >> costs[i];
a--;
ratings[i] = MXVAL+1-ratings[i];
indeg[a]++;
in_edges[a].push_back(i);
out_val[i] = a;
}
for (int i = 0; i < n; i++) {
if (!vis[i] && indeg[i] == 0) prune(i);
}
for (int i = 0; i < n; i++) {
if (!vis[i]) add_to(i);
}
//fin.close();
return ans;
}
void gen_case() {
int N = 6;
ofstream fout("report.in");
fout << N << "\n";
for (int i = 0; i < N; i++) {
fout << (1+(rand() % N)) << " " << (1+(rand() % 10)) << " " << (1+(rand() % 10)) << "\n";
}
fout.close();
}
signed main() {
// srand(time(0));
// vector<int> v;
ll ans = solve();
// ll b = bash(v);
// cout << ans << " " << b << endl;
// while (1) {
// gen_case();
// ll ans = solve();
// ll b = bash(v);
// assert(ans == b);
// }
cout << ans << "\n";
}
Compilation message
worst_reporter2.cpp: In function 'll bash(std::vector<long long int>&, ll)':
worst_reporter2.cpp:104:18: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
104 | if (vals.size() == n) {
| ~~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
7 ms |
5556 KB |
Output is correct |
6 |
Correct |
6 ms |
5588 KB |
Output is correct |
7 |
Correct |
6 ms |
5588 KB |
Output is correct |
8 |
Correct |
7 ms |
5676 KB |
Output is correct |
9 |
Correct |
6 ms |
5644 KB |
Output is correct |
10 |
Correct |
6 ms |
5588 KB |
Output is correct |
11 |
Correct |
5 ms |
5552 KB |
Output is correct |
12 |
Correct |
6 ms |
5460 KB |
Output is correct |
13 |
Correct |
5 ms |
5428 KB |
Output is correct |
14 |
Correct |
6 ms |
5488 KB |
Output is correct |
15 |
Correct |
6 ms |
5460 KB |
Output is correct |
16 |
Correct |
7 ms |
5684 KB |
Output is correct |
17 |
Correct |
7 ms |
5460 KB |
Output is correct |
18 |
Correct |
5 ms |
5332 KB |
Output is correct |
19 |
Correct |
7 ms |
5684 KB |
Output is correct |
20 |
Correct |
5 ms |
5668 KB |
Output is correct |
21 |
Correct |
5 ms |
5660 KB |
Output is correct |
22 |
Correct |
6 ms |
5972 KB |
Output is correct |
23 |
Correct |
5 ms |
5972 KB |
Output is correct |
24 |
Correct |
6 ms |
5676 KB |
Output is correct |
25 |
Correct |
6 ms |
5680 KB |
Output is correct |
26 |
Correct |
5 ms |
5460 KB |
Output is correct |
27 |
Correct |
6 ms |
5844 KB |
Output is correct |
28 |
Correct |
5 ms |
5716 KB |
Output is correct |
29 |
Correct |
6 ms |
5680 KB |
Output is correct |
30 |
Correct |
6 ms |
5972 KB |
Output is correct |
31 |
Correct |
6 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
7 ms |
5556 KB |
Output is correct |
6 |
Correct |
6 ms |
5588 KB |
Output is correct |
7 |
Correct |
6 ms |
5588 KB |
Output is correct |
8 |
Correct |
7 ms |
5676 KB |
Output is correct |
9 |
Correct |
6 ms |
5644 KB |
Output is correct |
10 |
Correct |
6 ms |
5588 KB |
Output is correct |
11 |
Correct |
5 ms |
5552 KB |
Output is correct |
12 |
Correct |
6 ms |
5460 KB |
Output is correct |
13 |
Correct |
5 ms |
5428 KB |
Output is correct |
14 |
Correct |
6 ms |
5488 KB |
Output is correct |
15 |
Correct |
6 ms |
5460 KB |
Output is correct |
16 |
Correct |
7 ms |
5684 KB |
Output is correct |
17 |
Correct |
7 ms |
5460 KB |
Output is correct |
18 |
Correct |
5 ms |
5332 KB |
Output is correct |
19 |
Correct |
7 ms |
5684 KB |
Output is correct |
20 |
Correct |
5 ms |
5668 KB |
Output is correct |
21 |
Correct |
5 ms |
5660 KB |
Output is correct |
22 |
Correct |
6 ms |
5972 KB |
Output is correct |
23 |
Correct |
5 ms |
5972 KB |
Output is correct |
24 |
Correct |
6 ms |
5676 KB |
Output is correct |
25 |
Correct |
6 ms |
5680 KB |
Output is correct |
26 |
Correct |
5 ms |
5460 KB |
Output is correct |
27 |
Correct |
6 ms |
5844 KB |
Output is correct |
28 |
Correct |
5 ms |
5716 KB |
Output is correct |
29 |
Correct |
6 ms |
5680 KB |
Output is correct |
30 |
Correct |
6 ms |
5972 KB |
Output is correct |
31 |
Correct |
6 ms |
5972 KB |
Output is correct |
32 |
Correct |
8 ms |
5684 KB |
Output is correct |
33 |
Correct |
343 ms |
34348 KB |
Output is correct |
34 |
Correct |
252 ms |
32884 KB |
Output is correct |
35 |
Correct |
337 ms |
32948 KB |
Output is correct |
36 |
Correct |
257 ms |
32844 KB |
Output is correct |
37 |
Correct |
166 ms |
30492 KB |
Output is correct |
38 |
Correct |
148 ms |
28364 KB |
Output is correct |
39 |
Correct |
135 ms |
24408 KB |
Output is correct |
40 |
Correct |
122 ms |
24400 KB |
Output is correct |
41 |
Correct |
88 ms |
24404 KB |
Output is correct |
42 |
Correct |
134 ms |
24596 KB |
Output is correct |
43 |
Correct |
128 ms |
24584 KB |
Output is correct |
44 |
Correct |
253 ms |
33520 KB |
Output is correct |
45 |
Correct |
173 ms |
21836 KB |
Output is correct |
46 |
Correct |
87 ms |
21196 KB |
Output is correct |
47 |
Correct |
198 ms |
33700 KB |
Output is correct |
48 |
Correct |
108 ms |
33704 KB |
Output is correct |
49 |
Correct |
93 ms |
33672 KB |
Output is correct |
50 |
Correct |
192 ms |
43920 KB |
Output is correct |
51 |
Correct |
95 ms |
43836 KB |
Output is correct |
52 |
Correct |
215 ms |
34580 KB |
Output is correct |
53 |
Correct |
105 ms |
34608 KB |
Output is correct |
54 |
Correct |
89 ms |
24528 KB |
Output is correct |
55 |
Correct |
169 ms |
33808 KB |
Output is correct |
56 |
Correct |
168 ms |
31804 KB |
Output is correct |
57 |
Correct |
136 ms |
33196 KB |
Output is correct |
58 |
Correct |
170 ms |
42444 KB |
Output is correct |
59 |
Correct |
156 ms |
42456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
7 ms |
5556 KB |
Output is correct |
6 |
Correct |
6 ms |
5588 KB |
Output is correct |
7 |
Correct |
6 ms |
5588 KB |
Output is correct |
8 |
Correct |
7 ms |
5676 KB |
Output is correct |
9 |
Correct |
6 ms |
5644 KB |
Output is correct |
10 |
Correct |
6 ms |
5588 KB |
Output is correct |
11 |
Correct |
5 ms |
5552 KB |
Output is correct |
12 |
Correct |
6 ms |
5460 KB |
Output is correct |
13 |
Correct |
5 ms |
5428 KB |
Output is correct |
14 |
Correct |
6 ms |
5488 KB |
Output is correct |
15 |
Correct |
6 ms |
5460 KB |
Output is correct |
16 |
Correct |
7 ms |
5684 KB |
Output is correct |
17 |
Correct |
7 ms |
5460 KB |
Output is correct |
18 |
Correct |
5 ms |
5332 KB |
Output is correct |
19 |
Correct |
7 ms |
5684 KB |
Output is correct |
20 |
Correct |
5 ms |
5668 KB |
Output is correct |
21 |
Correct |
5 ms |
5660 KB |
Output is correct |
22 |
Correct |
6 ms |
5972 KB |
Output is correct |
23 |
Correct |
5 ms |
5972 KB |
Output is correct |
24 |
Correct |
6 ms |
5676 KB |
Output is correct |
25 |
Correct |
6 ms |
5680 KB |
Output is correct |
26 |
Correct |
5 ms |
5460 KB |
Output is correct |
27 |
Correct |
6 ms |
5844 KB |
Output is correct |
28 |
Correct |
5 ms |
5716 KB |
Output is correct |
29 |
Correct |
6 ms |
5680 KB |
Output is correct |
30 |
Correct |
6 ms |
5972 KB |
Output is correct |
31 |
Correct |
6 ms |
5972 KB |
Output is correct |
32 |
Correct |
8 ms |
5684 KB |
Output is correct |
33 |
Correct |
343 ms |
34348 KB |
Output is correct |
34 |
Correct |
252 ms |
32884 KB |
Output is correct |
35 |
Correct |
337 ms |
32948 KB |
Output is correct |
36 |
Correct |
257 ms |
32844 KB |
Output is correct |
37 |
Correct |
166 ms |
30492 KB |
Output is correct |
38 |
Correct |
148 ms |
28364 KB |
Output is correct |
39 |
Correct |
135 ms |
24408 KB |
Output is correct |
40 |
Correct |
122 ms |
24400 KB |
Output is correct |
41 |
Correct |
88 ms |
24404 KB |
Output is correct |
42 |
Correct |
134 ms |
24596 KB |
Output is correct |
43 |
Correct |
128 ms |
24584 KB |
Output is correct |
44 |
Correct |
253 ms |
33520 KB |
Output is correct |
45 |
Correct |
173 ms |
21836 KB |
Output is correct |
46 |
Correct |
87 ms |
21196 KB |
Output is correct |
47 |
Correct |
198 ms |
33700 KB |
Output is correct |
48 |
Correct |
108 ms |
33704 KB |
Output is correct |
49 |
Correct |
93 ms |
33672 KB |
Output is correct |
50 |
Correct |
192 ms |
43920 KB |
Output is correct |
51 |
Correct |
95 ms |
43836 KB |
Output is correct |
52 |
Correct |
215 ms |
34580 KB |
Output is correct |
53 |
Correct |
105 ms |
34608 KB |
Output is correct |
54 |
Correct |
89 ms |
24528 KB |
Output is correct |
55 |
Correct |
169 ms |
33808 KB |
Output is correct |
56 |
Correct |
168 ms |
31804 KB |
Output is correct |
57 |
Correct |
136 ms |
33196 KB |
Output is correct |
58 |
Correct |
170 ms |
42444 KB |
Output is correct |
59 |
Correct |
156 ms |
42456 KB |
Output is correct |
60 |
Correct |
3 ms |
4948 KB |
Output is correct |
61 |
Correct |
3 ms |
4948 KB |
Output is correct |
62 |
Correct |
2 ms |
5028 KB |
Output is correct |
63 |
Correct |
3 ms |
4948 KB |
Output is correct |
64 |
Correct |
338 ms |
31548 KB |
Output is correct |
65 |
Correct |
250 ms |
31428 KB |
Output is correct |
66 |
Correct |
296 ms |
32016 KB |
Output is correct |
67 |
Correct |
244 ms |
31488 KB |
Output is correct |
68 |
Correct |
185 ms |
29152 KB |
Output is correct |
69 |
Correct |
160 ms |
27436 KB |
Output is correct |
70 |
Correct |
226 ms |
31484 KB |
Output is correct |
71 |
Correct |
140 ms |
22864 KB |
Output is correct |
72 |
Correct |
265 ms |
35324 KB |
Output is correct |
73 |
Correct |
135 ms |
22920 KB |
Output is correct |
74 |
Correct |
288 ms |
33820 KB |
Output is correct |
75 |
Correct |
183 ms |
33740 KB |
Output is correct |
76 |
Correct |
161 ms |
33748 KB |
Output is correct |
77 |
Correct |
206 ms |
36540 KB |
Output is correct |
78 |
Correct |
130 ms |
34540 KB |
Output is correct |
79 |
Correct |
283 ms |
34176 KB |
Output is correct |
80 |
Correct |
188 ms |
28748 KB |
Output is correct |
81 |
Correct |
151 ms |
27424 KB |
Output is correct |
82 |
Correct |
201 ms |
35584 KB |
Output is correct |
83 |
Correct |
95 ms |
35276 KB |
Output is correct |
84 |
Correct |
242 ms |
35000 KB |
Output is correct |
85 |
Correct |
230 ms |
34784 KB |
Output is correct |
86 |
Correct |
230 ms |
31564 KB |
Output is correct |
87 |
Correct |
236 ms |
34988 KB |
Output is correct |
88 |
Correct |
230 ms |
34844 KB |
Output is correct |