///
/// ♪ Pizza mozzarella rella rella rella... ♪
///
#define _USE_MATH_DEFINES
#define FAST ios::sync_with_stdio(false),cin.tie(0);
#include <bits/stdc++.h>
#define Loop(x, l, r) for(int x = (l); x < (r); ++x)
#define LoopR(x, l, r) for(int x = (r)-1; x >= (l); --x)
#define all(x) x.begin(), x.end()
#define Kill(x) exit((cout << (x) << '\n', 0))
#define YN(flag) ((flag)? "YES": "NO")
#define F first
#define S second
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> pii;
typedef std::pair<ll ,ll > pll;
using namespace std;
const int N = 300'010;
vector<pll> A[N];
int n, m;
struct obj
{
priority_queue<ll>* pq;
ll r, w;
obj(){r=w=0;pq=new priority_queue<ll>;pq->push(0);pq->push(0);}
void add(ll x)
{
r += x;
x += pq->top();
pq->pop();
pq->push(x);
}
void add(obj x)
{
w += x.w;
if(r > x.r) swap(pq,x.pq);
if(x.pq->top() <= r) r = min(r, x.r);
else
{
w += x.pq->top() - r;
r = x.pq->top();
x.pq->pop();
}
if(pq->size() < x.pq->size()) swap(pq, x.pq);
while(x.pq->size()){pq->push(x.pq->top()); x.pq->pop();}
}
};
obj dfs(int v)
{
if(v >= n) return obj();
obj o; o.r = 1e18;
for(auto [u, w] : A[v])
{
auto o2 = dfs(u);
o2.add(w);
o.add(o2);
}
return o;
}
int main()
{
FAST;
cin >> n >> m;
Loop(i,1,n+m)
{
ll v, c;
cin >> v >> c;
--v;
A[v].emplace_back(i,c);
}
cout << dfs(0).w;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Incorrect |
4 ms |
7244 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Incorrect |
4 ms |
7244 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Incorrect |
4 ms |
7244 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7244 KB |
Output is correct |
2 |
Incorrect |
4 ms |
7244 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |