#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define IOS ios::sync_with_stdio(false);; cin.tie(NULL)
void selfMax(long long& a, long long b){
a = max(a, b);
}
void selfMin(long long& a, long long b){
a = min(a, b);
}
template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& x) {return in >> x.first >> x.second;}
template<typename T1, typename T2> ostream& operator<<(ostream& out, const pair<T1, T2>& x) {return out << x.first << ' ' << x.second;}
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {for(auto &x : a) out << x << ' '; return out;};
template<typename T> ostream& operator<<(ostream& out, vector<vector<T> >& a) {for(auto &x : a) out << x << '\n'; return out;};
template<typename T1, typename T2> ostream& operator<<(ostream& out, vector<pair<T1, T2> >& a) {for(auto &x : a) out << x << '\n'; return out;};
/*
Use DSU dsu(N);
*/
struct DSU {
vector<long long> adj2;
DSU(long long N) { adj2 = vector<long long>(N, -1); }
// get representive component (uses path compression)
long long get(long long x) { return adj2[x] < 0 ? x : adj2[x] = get(adj2[x]); }
bool same_set(long long a, long long b) { return get(a) == get(b); }
long long size(long long x) { return -adj2[get(x)]; }
bool unite(long long x, long long y) { // union by size
x = get(x), y = get(y);
if (x == y) return false;
if (adj2[x] > adj2[y]) swap(x, y);
adj2[x] += adj2[y]; adj2[y] = x;
return true;
}
};
/*
Run with Bit<long long> BIT
*/
template <class T> class BIT {
private:
long long size;
vector<T> bit;
vector<T> arr;
public:
BIT(long long size) : size(size), bit(size + 1), arr(size) {}
void set(long long ind, long long val) { add(ind, val - arr[ind]); }
void add(long long ind, long long val) {
arr[ind] += val;
ind++;
for (; ind <= size; ind += ind & -ind) { bit[ind] += val; }
}
T pref_sum(long long ind) {
ind++;
T total = 0;
for (; ind > 0; ind -= ind & -ind) { total += bit[ind]; }
return total;
}
};
/*
Change Comb for specific Seg tree probs, but run with Seg<long long> Seg;
*/
template<class T> struct Seg {
const T ID = 1e18; T comb(T a, T b) { return min(a,b); }
long long n; vector<T> seg;
void init(long long _n) { n = _n; seg.assign(2*n,ID); }
void pull(long long p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }
void upd(long long p, T val) {
seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); }
T query(long long l, long long r) {
T ra = ID, rb = ID;
for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
if (l&1) ra = comb(ra,seg[l++]);
if (r&1) rb = comb(seg[--r],rb);
}
return comb(ra,rb);
}
};
const int N = 200001;
const int M = 501;
vector<int> adj[N];
int dp[N][M];
int values[N];
int timeVal = 0;
int timeIn[N];
int timeOut[N];
int val[N];
void dfs(int u){
timeVal++;
timeIn[u] = timeVal;
val[timeVal] = values[u];
dp[timeVal][1] = values[u];
for(int v : adj[u]){
dfs(v);
}
timeOut[timeIn[u]] = timeVal;
}
int main(){
int n, m;
cin >> n >> m;
int start = -1;
for(int i = 1; i <= n; i++){
int p;
cin >> p;
cin >> values[i];
if(p == 0){
start = i;
}
else{
adj[p].push_back(i);
}
}
dfs(start);
int ans = 0;
for(int i = timeVal; i >= 1; i--){
for(int j = 1; j <= m; ++j){
if(i + 1 <= timeVal){
dp[i][j] = max(dp[i][j], dp[i + 1][j]);
}
if(timeOut[i] + 1 <= timeVal){
dp[i][j] = max(dp[i][j] , dp[timeOut[i] + 1][j - 1] + val[i]);
}
if(j == m){
ans = max(ans, dp[i][j]);
}
}
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5004 KB |
Output is correct |
3 |
Correct |
2 ms |
5204 KB |
Output is correct |
4 |
Correct |
15 ms |
22796 KB |
Output is correct |
5 |
Correct |
16 ms |
24988 KB |
Output is correct |
6 |
Correct |
17 ms |
24956 KB |
Output is correct |
7 |
Correct |
272 ms |
299184 KB |
Output is correct |
8 |
Correct |
248 ms |
299096 KB |
Output is correct |
9 |
Correct |
319 ms |
363220 KB |
Output is correct |
10 |
Correct |
373 ms |
400904 KB |
Output is correct |