Submission #1085563

#TimeUsernameProblemLanguageResultExecution timeMemory
1085563SunbaeBiochips (IZhO12_biochips)C++17
20 / 100
2071 ms400980 KiB
#include <bits/stdc++.h> #define z exit(0) using namespace std; const int N = 2e5 + 5, inf = 1e8; vector<int> g[N]; int x[N], tin[N], tout[N], timer = 0, V[N], dp[N][501]; void dfs(int u, int p){ tin[u] = timer++; for(int v: g[u]) dfs(v, u); tout[u] = timer++; } bool cmp(int i, int j){ return tout[i] < tout[j]; } signed main(){ int n, m, r = 0; scanf("%d %d", &n, &m); for(int v = 0, u; v<n; ++v){ scanf("%d %d", &u, x+v); --u; if(u != -1) g[u].push_back(v); else r = v; V[v] = v; fill(dp[v], dp[v]+m+1, -inf); } dfs(r, r); sort(V, V+n, cmp); dp[0][0] = 0; dp[0][1] = x[V[0]]; int ans = dp[0][m]; for(int i = 1; i<n; ++i){ for(int k = 0; k<=m; ++k){ dp[i][k] = max(dp[i][k], dp[i-1][k]); int low = 0, high = i-1, idx = -1; while(low <= high){ int mid = low + ((high-low)>>1); if(tout[V[mid]] < tin[V[i]]) low = mid+1, idx = mid; else high = mid-1; } if(idx != -1) dp[i][k] = max(dp[i][k], dp[idx][k-1] + x[V[i]]); } ans = max(ans, dp[i][m]); } printf("%d", ans); }

Compilation message (stderr)

biochips.cpp: In function 'int main()':
biochips.cpp:14:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |  int n, m, r = 0; scanf("%d %d", &n, &m);
      |                   ~~~~~^~~~~~~~~~~~~~~~~
biochips.cpp:16:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |   scanf("%d %d", &u, x+v); --u;
      |   ~~~~~^~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...