/*
* Author: Gyeonggeun Kim(kriii)
* Time Complexity: O(N^2)
*/
#include <stdio.h>
#include <vector>
using namespace std;
const long long dec_cost = 1, inc_cost = 1;
struct envelope
{
envelope(long long s, long long w)
{
slope = s; width = w;
}
long long slope, width;
};
struct conv_func
{
conv_func(){
base_cost = 0;
func.clear();
func.push_back(envelope(1e18,-1));
}
long long base_cost;
vector<envelope> func;
void append(long long width)
{
base_cost += width * dec_cost;
vector<envelope> new_func;
int i = 0;
while (func[i].slope < -dec_cost) new_func.push_back(func[i++]);
if (func[i].slope == -dec_cost) func[i].width += width;
else new_func.push_back(envelope(-dec_cost,width));
while (func[i].slope < inc_cost) new_func.push_back(func[i++]);
new_func.push_back(envelope(inc_cost,-1));
func = new_func;
}
conv_func operator +(conv_func b){
auto &a = (*this);
conv_func ret;
auto &func = ret.func;
func.clear();
ret.base_cost = a.base_cost + b.base_cost;
long long ax = 0, bx = 0;
int i = 0, j = 0;
bool stop = 0;
while (!stop){
auto &p = a.func[i], &q = b.func[j];
envelope next_env(p.slope+q.slope,-1);
long long next_ax = ax + p.width;
long long next_bx = bx + q.width;
long long prev_x = max(ax,bx);
if (p.width == -1 && q.width == -1) stop = 1;
else if (q.width == -1){
next_env.width = next_ax - prev_x;
i++, ax = next_ax;
}
else if (p.width == -1){
next_env.width = next_bx - prev_x;
j++, bx = next_bx;
}
else{
long long next_x = min(next_ax,next_bx);
next_env.width = next_x - prev_x;
if (next_x == next_ax) i++, ax = next_ax;
if (next_x == next_bx) j++, bx = next_bx;
}
func.push_back(next_env);
}
return ret;
}
};
int N,M,V;
vector<pair<int, int> > G[100010];
conv_func dfs(int x)
{
conv_func ret;
for (auto p : G[x]){
int y = p.first, c = p.second;
auto chd = dfs(y);
chd.append(c);
if (ret.func.size() == 1) ret = chd;
else ret = ret + chd;
}
return ret;
}
int main()
{
scanf ("%d %d",&N,&M);
V = N + M;
for (int i=2;i<=V;i++){
int x,c; scanf ("%d %d",&x,&c);
G[x].push_back(make_pair(i,c));
}
auto res = dfs(1);
long long base = res.base_cost;
long long ans = base;
for (int i=0;i<res.func.size();i++){
auto e = res.func[i];
if (e.width == -1) break;
base += e.slope * e.width;
if (ans > base)
ans = base;
}
printf ("%lld\n",ans);
return 0;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:114:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<envelope>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for (int i=0;i<res.func.size();i++){
| ~^~~~~~~~~~~~~~~~
fireworks.cpp:103:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | scanf ("%d %d",&N,&M);
| ~~~~~~^~~~~~~~~~~~~~~
fireworks.cpp:107:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | int x,c; scanf ("%d %d",&x,&c);
| ~~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2508 KB |
Output is correct |
2 |
Correct |
1 ms |
2508 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
1 ms |
2508 KB |
Output is correct |
8 |
Correct |
1 ms |
2636 KB |
Output is correct |
9 |
Correct |
1 ms |
2508 KB |
Output is correct |
10 |
Correct |
1 ms |
2508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2508 KB |
Output is correct |
2 |
Correct |
2 ms |
2616 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2640 KB |
Output is correct |
5 |
Correct |
1 ms |
2632 KB |
Output is correct |
6 |
Correct |
2 ms |
2628 KB |
Output is correct |
7 |
Correct |
2 ms |
2632 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2632 KB |
Output is correct |
13 |
Correct |
2 ms |
2628 KB |
Output is correct |
14 |
Correct |
2 ms |
2640 KB |
Output is correct |
15 |
Correct |
1 ms |
2640 KB |
Output is correct |
16 |
Correct |
2 ms |
2632 KB |
Output is correct |
17 |
Correct |
2 ms |
2640 KB |
Output is correct |
18 |
Correct |
2 ms |
2628 KB |
Output is correct |
19 |
Correct |
2 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2508 KB |
Output is correct |
2 |
Correct |
1 ms |
2508 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
1 ms |
2508 KB |
Output is correct |
8 |
Correct |
1 ms |
2636 KB |
Output is correct |
9 |
Correct |
1 ms |
2508 KB |
Output is correct |
10 |
Correct |
1 ms |
2508 KB |
Output is correct |
11 |
Correct |
1 ms |
2508 KB |
Output is correct |
12 |
Correct |
2 ms |
2616 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2640 KB |
Output is correct |
15 |
Correct |
1 ms |
2632 KB |
Output is correct |
16 |
Correct |
2 ms |
2628 KB |
Output is correct |
17 |
Correct |
2 ms |
2632 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2640 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
2 ms |
2632 KB |
Output is correct |
23 |
Correct |
2 ms |
2628 KB |
Output is correct |
24 |
Correct |
2 ms |
2640 KB |
Output is correct |
25 |
Correct |
1 ms |
2640 KB |
Output is correct |
26 |
Correct |
2 ms |
2632 KB |
Output is correct |
27 |
Correct |
2 ms |
2640 KB |
Output is correct |
28 |
Correct |
2 ms |
2628 KB |
Output is correct |
29 |
Correct |
2 ms |
2640 KB |
Output is correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2640 KB |
Output is correct |
32 |
Correct |
4 ms |
2644 KB |
Output is correct |
33 |
Correct |
3 ms |
2768 KB |
Output is correct |
34 |
Correct |
3 ms |
2768 KB |
Output is correct |
35 |
Correct |
5 ms |
2768 KB |
Output is correct |
36 |
Correct |
7 ms |
2896 KB |
Output is correct |
37 |
Correct |
5 ms |
2900 KB |
Output is correct |
38 |
Correct |
6 ms |
3028 KB |
Output is correct |
39 |
Correct |
7 ms |
2884 KB |
Output is correct |
40 |
Correct |
4 ms |
4052 KB |
Output is correct |
41 |
Correct |
4 ms |
4052 KB |
Output is correct |
42 |
Correct |
4 ms |
2888 KB |
Output is correct |
43 |
Correct |
26 ms |
3468 KB |
Output is correct |
44 |
Correct |
28 ms |
3344 KB |
Output is correct |
45 |
Correct |
28 ms |
3320 KB |
Output is correct |
46 |
Correct |
11 ms |
2956 KB |
Output is correct |
47 |
Correct |
11 ms |
2896 KB |
Output is correct |
48 |
Correct |
40 ms |
2848 KB |
Output is correct |
49 |
Correct |
44 ms |
2856 KB |
Output is correct |
50 |
Correct |
72 ms |
2888 KB |
Output is correct |
51 |
Correct |
68 ms |
2828 KB |
Output is correct |
52 |
Correct |
63 ms |
3020 KB |
Output is correct |
53 |
Correct |
44 ms |
2896 KB |
Output is correct |
54 |
Correct |
126 ms |
3016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2508 KB |
Output is correct |
2 |
Correct |
1 ms |
2508 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
1 ms |
2508 KB |
Output is correct |
8 |
Correct |
1 ms |
2636 KB |
Output is correct |
9 |
Correct |
1 ms |
2508 KB |
Output is correct |
10 |
Correct |
1 ms |
2508 KB |
Output is correct |
11 |
Correct |
1 ms |
2508 KB |
Output is correct |
12 |
Correct |
2 ms |
2616 KB |
Output is correct |
13 |
Correct |
2 ms |
2640 KB |
Output is correct |
14 |
Correct |
2 ms |
2640 KB |
Output is correct |
15 |
Correct |
1 ms |
2632 KB |
Output is correct |
16 |
Correct |
2 ms |
2628 KB |
Output is correct |
17 |
Correct |
2 ms |
2632 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2640 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
2 ms |
2632 KB |
Output is correct |
23 |
Correct |
2 ms |
2628 KB |
Output is correct |
24 |
Correct |
2 ms |
2640 KB |
Output is correct |
25 |
Correct |
1 ms |
2640 KB |
Output is correct |
26 |
Correct |
2 ms |
2632 KB |
Output is correct |
27 |
Correct |
2 ms |
2640 KB |
Output is correct |
28 |
Correct |
2 ms |
2628 KB |
Output is correct |
29 |
Correct |
2 ms |
2640 KB |
Output is correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
31 |
Correct |
2 ms |
2640 KB |
Output is correct |
32 |
Correct |
4 ms |
2644 KB |
Output is correct |
33 |
Correct |
3 ms |
2768 KB |
Output is correct |
34 |
Correct |
3 ms |
2768 KB |
Output is correct |
35 |
Correct |
5 ms |
2768 KB |
Output is correct |
36 |
Correct |
7 ms |
2896 KB |
Output is correct |
37 |
Correct |
5 ms |
2900 KB |
Output is correct |
38 |
Correct |
6 ms |
3028 KB |
Output is correct |
39 |
Correct |
7 ms |
2884 KB |
Output is correct |
40 |
Correct |
4 ms |
4052 KB |
Output is correct |
41 |
Correct |
4 ms |
4052 KB |
Output is correct |
42 |
Correct |
4 ms |
2888 KB |
Output is correct |
43 |
Correct |
26 ms |
3468 KB |
Output is correct |
44 |
Correct |
28 ms |
3344 KB |
Output is correct |
45 |
Correct |
28 ms |
3320 KB |
Output is correct |
46 |
Correct |
11 ms |
2956 KB |
Output is correct |
47 |
Correct |
11 ms |
2896 KB |
Output is correct |
48 |
Correct |
40 ms |
2848 KB |
Output is correct |
49 |
Correct |
44 ms |
2856 KB |
Output is correct |
50 |
Correct |
72 ms |
2888 KB |
Output is correct |
51 |
Correct |
68 ms |
2828 KB |
Output is correct |
52 |
Correct |
63 ms |
3020 KB |
Output is correct |
53 |
Correct |
44 ms |
2896 KB |
Output is correct |
54 |
Correct |
126 ms |
3016 KB |
Output is correct |
55 |
Correct |
19 ms |
3424 KB |
Output is correct |
56 |
Correct |
98 ms |
5784 KB |
Output is correct |
57 |
Correct |
245 ms |
7824 KB |
Output is correct |
58 |
Runtime error |
36 ms |
11644 KB |
Execution killed with signal 11 |
59 |
Halted |
0 ms |
0 KB |
- |