/*
* 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 between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<res.func.size();i++){
^
fireworks.cpp:103:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d %d",&N,&M);
^
fireworks.cpp:107:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x,c; scanf ("%d %d",&x,&c);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4280 KB |
Output is correct |
2 |
Correct |
0 ms |
4280 KB |
Output is correct |
3 |
Correct |
0 ms |
4280 KB |
Output is correct |
4 |
Correct |
0 ms |
4280 KB |
Output is correct |
5 |
Correct |
0 ms |
4280 KB |
Output is correct |
6 |
Correct |
0 ms |
4280 KB |
Output is correct |
7 |
Correct |
0 ms |
4280 KB |
Output is correct |
8 |
Correct |
0 ms |
4280 KB |
Output is correct |
9 |
Correct |
0 ms |
4280 KB |
Output is correct |
10 |
Correct |
0 ms |
4280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4280 KB |
Output is correct |
2 |
Correct |
0 ms |
4280 KB |
Output is correct |
3 |
Correct |
0 ms |
4280 KB |
Output is correct |
4 |
Correct |
0 ms |
4280 KB |
Output is correct |
5 |
Correct |
0 ms |
4280 KB |
Output is correct |
6 |
Correct |
0 ms |
4280 KB |
Output is correct |
7 |
Correct |
0 ms |
4280 KB |
Output is correct |
8 |
Correct |
0 ms |
4280 KB |
Output is correct |
9 |
Correct |
0 ms |
4280 KB |
Output is correct |
10 |
Correct |
0 ms |
4280 KB |
Output is correct |
11 |
Correct |
0 ms |
4280 KB |
Output is correct |
12 |
Correct |
0 ms |
4280 KB |
Output is correct |
13 |
Correct |
0 ms |
4280 KB |
Output is correct |
14 |
Correct |
0 ms |
4280 KB |
Output is correct |
15 |
Correct |
0 ms |
4280 KB |
Output is correct |
16 |
Correct |
0 ms |
4280 KB |
Output is correct |
17 |
Correct |
0 ms |
4280 KB |
Output is correct |
18 |
Correct |
0 ms |
4280 KB |
Output is correct |
19 |
Correct |
0 ms |
4280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4280 KB |
Output is correct |
2 |
Correct |
0 ms |
4280 KB |
Output is correct |
3 |
Correct |
0 ms |
4280 KB |
Output is correct |
4 |
Correct |
0 ms |
4280 KB |
Output is correct |
5 |
Correct |
0 ms |
4280 KB |
Output is correct |
6 |
Correct |
0 ms |
4280 KB |
Output is correct |
7 |
Correct |
0 ms |
4280 KB |
Output is correct |
8 |
Correct |
0 ms |
4280 KB |
Output is correct |
9 |
Correct |
0 ms |
4280 KB |
Output is correct |
10 |
Correct |
0 ms |
4280 KB |
Output is correct |
11 |
Correct |
0 ms |
4280 KB |
Output is correct |
12 |
Correct |
0 ms |
4280 KB |
Output is correct |
13 |
Correct |
0 ms |
4280 KB |
Output is correct |
14 |
Correct |
0 ms |
4280 KB |
Output is correct |
15 |
Correct |
0 ms |
4280 KB |
Output is correct |
16 |
Correct |
0 ms |
4280 KB |
Output is correct |
17 |
Correct |
0 ms |
4280 KB |
Output is correct |
18 |
Correct |
0 ms |
4280 KB |
Output is correct |
19 |
Correct |
0 ms |
4280 KB |
Output is correct |
20 |
Correct |
0 ms |
4280 KB |
Output is correct |
21 |
Correct |
0 ms |
4280 KB |
Output is correct |
22 |
Correct |
0 ms |
4280 KB |
Output is correct |
23 |
Correct |
0 ms |
4280 KB |
Output is correct |
24 |
Correct |
0 ms |
4280 KB |
Output is correct |
25 |
Correct |
0 ms |
4280 KB |
Output is correct |
26 |
Correct |
0 ms |
4280 KB |
Output is correct |
27 |
Correct |
0 ms |
4280 KB |
Output is correct |
28 |
Correct |
0 ms |
4280 KB |
Output is correct |
29 |
Correct |
0 ms |
4280 KB |
Output is correct |
30 |
Correct |
0 ms |
4280 KB |
Output is correct |
31 |
Correct |
3 ms |
4412 KB |
Output is correct |
32 |
Correct |
3 ms |
4416 KB |
Output is correct |
33 |
Correct |
3 ms |
4412 KB |
Output is correct |
34 |
Correct |
3 ms |
4412 KB |
Output is correct |
35 |
Correct |
6 ms |
4412 KB |
Output is correct |
36 |
Correct |
6 ms |
4412 KB |
Output is correct |
37 |
Correct |
3 ms |
4544 KB |
Output is correct |
38 |
Correct |
6 ms |
4544 KB |
Output is correct |
39 |
Correct |
6 ms |
4552 KB |
Output is correct |
40 |
Correct |
3 ms |
5516 KB |
Output is correct |
41 |
Correct |
3 ms |
5512 KB |
Output is correct |
42 |
Correct |
0 ms |
4412 KB |
Output is correct |
43 |
Correct |
33 ms |
4984 KB |
Output is correct |
44 |
Correct |
26 ms |
4824 KB |
Output is correct |
45 |
Correct |
29 ms |
4824 KB |
Output is correct |
46 |
Correct |
6 ms |
4672 KB |
Output is correct |
47 |
Correct |
6 ms |
4676 KB |
Output is correct |
48 |
Correct |
29 ms |
4612 KB |
Output is correct |
49 |
Correct |
33 ms |
4548 KB |
Output is correct |
50 |
Correct |
49 ms |
4628 KB |
Output is correct |
51 |
Correct |
49 ms |
4580 KB |
Output is correct |
52 |
Correct |
43 ms |
4640 KB |
Output is correct |
53 |
Correct |
29 ms |
4660 KB |
Output is correct |
54 |
Correct |
89 ms |
4840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4280 KB |
Output is correct |
2 |
Correct |
0 ms |
4280 KB |
Output is correct |
3 |
Correct |
0 ms |
4280 KB |
Output is correct |
4 |
Correct |
0 ms |
4280 KB |
Output is correct |
5 |
Correct |
0 ms |
4280 KB |
Output is correct |
6 |
Correct |
0 ms |
4280 KB |
Output is correct |
7 |
Correct |
0 ms |
4280 KB |
Output is correct |
8 |
Correct |
0 ms |
4280 KB |
Output is correct |
9 |
Correct |
0 ms |
4280 KB |
Output is correct |
10 |
Correct |
0 ms |
4280 KB |
Output is correct |
11 |
Correct |
0 ms |
4280 KB |
Output is correct |
12 |
Correct |
0 ms |
4280 KB |
Output is correct |
13 |
Correct |
0 ms |
4280 KB |
Output is correct |
14 |
Correct |
0 ms |
4280 KB |
Output is correct |
15 |
Correct |
0 ms |
4280 KB |
Output is correct |
16 |
Correct |
0 ms |
4280 KB |
Output is correct |
17 |
Correct |
0 ms |
4280 KB |
Output is correct |
18 |
Correct |
0 ms |
4280 KB |
Output is correct |
19 |
Correct |
0 ms |
4280 KB |
Output is correct |
20 |
Correct |
0 ms |
4280 KB |
Output is correct |
21 |
Correct |
0 ms |
4280 KB |
Output is correct |
22 |
Correct |
0 ms |
4280 KB |
Output is correct |
23 |
Correct |
0 ms |
4280 KB |
Output is correct |
24 |
Correct |
0 ms |
4280 KB |
Output is correct |
25 |
Correct |
0 ms |
4280 KB |
Output is correct |
26 |
Correct |
0 ms |
4280 KB |
Output is correct |
27 |
Correct |
0 ms |
4280 KB |
Output is correct |
28 |
Correct |
0 ms |
4280 KB |
Output is correct |
29 |
Correct |
0 ms |
4280 KB |
Output is correct |
30 |
Correct |
0 ms |
4280 KB |
Output is correct |
31 |
Correct |
3 ms |
4412 KB |
Output is correct |
32 |
Correct |
3 ms |
4416 KB |
Output is correct |
33 |
Correct |
3 ms |
4412 KB |
Output is correct |
34 |
Correct |
3 ms |
4412 KB |
Output is correct |
35 |
Correct |
6 ms |
4412 KB |
Output is correct |
36 |
Correct |
6 ms |
4412 KB |
Output is correct |
37 |
Correct |
3 ms |
4544 KB |
Output is correct |
38 |
Correct |
6 ms |
4544 KB |
Output is correct |
39 |
Correct |
6 ms |
4552 KB |
Output is correct |
40 |
Correct |
3 ms |
5516 KB |
Output is correct |
41 |
Correct |
3 ms |
5512 KB |
Output is correct |
42 |
Correct |
0 ms |
4412 KB |
Output is correct |
43 |
Correct |
33 ms |
4984 KB |
Output is correct |
44 |
Correct |
26 ms |
4824 KB |
Output is correct |
45 |
Correct |
29 ms |
4824 KB |
Output is correct |
46 |
Correct |
6 ms |
4672 KB |
Output is correct |
47 |
Correct |
6 ms |
4676 KB |
Output is correct |
48 |
Correct |
29 ms |
4612 KB |
Output is correct |
49 |
Correct |
33 ms |
4548 KB |
Output is correct |
50 |
Correct |
49 ms |
4628 KB |
Output is correct |
51 |
Correct |
49 ms |
4580 KB |
Output is correct |
52 |
Correct |
43 ms |
4640 KB |
Output is correct |
53 |
Correct |
29 ms |
4660 KB |
Output is correct |
54 |
Correct |
89 ms |
4840 KB |
Output is correct |
55 |
Correct |
19 ms |
4872 KB |
Output is correct |
56 |
Correct |
119 ms |
6400 KB |
Output is correct |
57 |
Correct |
269 ms |
8124 KB |
Output is correct |
58 |
Runtime error |
46 ms |
6572 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
59 |
Halted |
0 ms |
0 KB |
- |