Submission #917475

# Submission time Handle Problem Language Result Execution time Memory
917475 2024-01-28T09:26:11 Z hariaakas646 Fireworks (APIO16_fireworks) C++17
100 / 100
158 ms 91984 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define scd(t) scanf("%d", &t)
#define sclld(t) scanf("%lld", &t)
#define forr(i, l, r) for(int i=l; i<r; i++)
#define frange(i, l) forr(i, 0, l)
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
 
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef long long lli;
typedef vector<vi> vvi;
typedef vector<lli> vll;
typedef vector<bool> vb;
typedef set<int> seti;
typedef multiset<int> mseti;
 
void usaco()
{
    freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
}
 
int n, m;
 
vector<vector<pair<int, lli>>> graph;
// vll dp;
 
struct Data {

    lli a = 0;
    lli b = 0;

    priority_queue<lli> pq;

};

Data dfs(int x, lli c) {
    if(x > n) {
        Data out;
        out.pq.push(c);
        out.pq.push(c);
        out.a = 1;
        out.b = -c;
        return out;
    }
    Data out;
    for(auto e : graph[x]) {
        auto v = dfs(e.f, e.s);
        out.a += v.a;
        out.b += v.b;
        if(out.pq.size() < v.pq.size()) swap(out.pq, v.pq);
        while(v.pq.size()) {
            lli x = v.pq.top();
            v.pq.pop();
            out.pq.push(x);
        }
    }
    while(out.a > 1) {
        out.a--;
        out.b += out.pq.top();
        out.pq.pop();
    }

    lli v1 = out.pq.top();
    out.pq.pop();
    lli v0 = out.pq.top();
    out.pq.pop();
    out.pq.push(v1 + c);
    out.pq.push(v0 + c);
    out.b -= c;
    return out;
}
 
int main() {
    // usaco();
    
    scd(n);
    scd(m);
 
    graph = vector<vector<pair<int, lli>>>(n+1);
 
    forr(i, 2, n+m+1) {
        int pa;
        lli c;
        scd(pa);
        sclld(c);
        graph[pa].pb(mp(i, c));
    }
    Data out = dfs(1, 0);
 
    while(out.a > 0) {
        out.a--;
        out.b += out.pq.top();
        out.pq.pop();
    }

    printf("%lld", out.b);

}

Compilation message

fireworks.cpp: In function 'void usaco()':
fireworks.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
fireworks.cpp:84:5: note: in expansion of macro 'scd'
   84 |     scd(n);
      |     ^~~
fireworks.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
fireworks.cpp:85:5: note: in expansion of macro 'scd'
   85 |     scd(m);
      |     ^~~
fireworks.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
fireworks.cpp:92:9: note: in expansion of macro 'scd'
   92 |         scd(pa);
      |         ^~~
fireworks.cpp:6:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    6 | #define sclld(t) scanf("%lld", &t)
      |                  ~~~~~^~~~~~~~~~~~
fireworks.cpp:93:9: note: in expansion of macro 'sclld'
   93 |         sclld(c);
      |         ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 432 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 432 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 436 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 432 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 432 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 436 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 448 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 2 ms 600 KB Output is correct
36 Correct 2 ms 584 KB Output is correct
37 Correct 2 ms 600 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 3 ms 708 KB Output is correct
40 Correct 2 ms 1728 KB Output is correct
41 Correct 2 ms 1728 KB Output is correct
42 Correct 2 ms 760 KB Output is correct
43 Correct 2 ms 1116 KB Output is correct
44 Correct 3 ms 1156 KB Output is correct
45 Correct 4 ms 1116 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 444 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 2 ms 604 KB Output is correct
52 Correct 2 ms 600 KB Output is correct
53 Correct 2 ms 684 KB Output is correct
54 Correct 2 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 432 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 432 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 436 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 448 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 2 ms 600 KB Output is correct
36 Correct 2 ms 584 KB Output is correct
37 Correct 2 ms 600 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 3 ms 708 KB Output is correct
40 Correct 2 ms 1728 KB Output is correct
41 Correct 2 ms 1728 KB Output is correct
42 Correct 2 ms 760 KB Output is correct
43 Correct 2 ms 1116 KB Output is correct
44 Correct 3 ms 1156 KB Output is correct
45 Correct 4 ms 1116 KB Output is correct
46 Correct 2 ms 604 KB Output is correct
47 Correct 2 ms 604 KB Output is correct
48 Correct 2 ms 444 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 604 KB Output is correct
51 Correct 2 ms 604 KB Output is correct
52 Correct 2 ms 600 KB Output is correct
53 Correct 2 ms 684 KB Output is correct
54 Correct 2 ms 600 KB Output is correct
55 Correct 6 ms 1304 KB Output is correct
56 Correct 18 ms 3640 KB Output is correct
57 Correct 42 ms 5916 KB Output is correct
58 Correct 50 ms 7328 KB Output is correct
59 Correct 58 ms 9492 KB Output is correct
60 Correct 72 ms 11476 KB Output is correct
61 Correct 102 ms 13596 KB Output is correct
62 Correct 112 ms 14704 KB Output is correct
63 Correct 111 ms 17196 KB Output is correct
64 Correct 149 ms 17876 KB Output is correct
65 Correct 112 ms 91984 KB Output is correct
66 Correct 118 ms 91984 KB Output is correct
67 Correct 77 ms 21832 KB Output is correct
68 Correct 148 ms 52944 KB Output is correct
69 Correct 158 ms 47792 KB Output is correct
70 Correct 154 ms 47728 KB Output is correct
71 Correct 137 ms 17700 KB Output is correct
72 Correct 121 ms 17980 KB Output is correct
73 Correct 107 ms 15552 KB Output is correct
74 Correct 107 ms 14928 KB Output is correct
75 Correct 110 ms 15088 KB Output is correct
76 Correct 115 ms 14308 KB Output is correct
77 Correct 116 ms 15736 KB Output is correct
78 Correct 141 ms 14880 KB Output is correct
79 Correct 105 ms 22444 KB Output is correct