Advertisement
tumaryui

Untitled

May 25th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. //#pragma GCC optimize("Ofast")
  3. //#pragma GCC target("avx,avx2,fma")
  4. //#pragma GCC optimization ("unroll-loops")
  5.  
  6. #define int long long
  7. #define pb push_back
  8. #define all(s) s.begin(),s.end()
  9. #define pii pair<int,int>
  10. #define fr first
  11. #define sc second
  12. #define bst ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  13. #define endl "\n"
  14. #define no cout << "NO" << endl;
  15. #define yes cout << "YES" << endl;
  16.  
  17. using namespace std;
  18.  
  19. const int N = 5e5 + 10, mod = 1e9 + 7, inf = 1e18 + 7, logn = 23;
  20. const double pi = acos(-1);
  21.  
  22. int up[32][N], c[N], s[N];
  23. int n;
  24. void build() {
  25. for(int i = 1; i <= 30; i++) {
  26. for(int j = 1; j <= n; j++) {
  27. up[i][j] = up[i - 1][up[i - 1][j]];
  28. }
  29. }
  30. }
  31.  
  32. void solve() {
  33. freopen("F.in", "r", stdin);
  34. freopen("F.out", "w", stdout);
  35.  
  36. cin >> n;
  37. for(int i = 1; i <= n; i++) {
  38. cin >> up[0][i];
  39. }
  40. for(int i = 1; i <= n; i++) {
  41. cin >> c[i];
  42. }
  43.  
  44. build();
  45. int ans = 0;
  46. for(int i = 1; i <= n; i++) {
  47. s[up[30][i]] += c[i];
  48. }
  49. for(int i = 1; i <= n; i++) {
  50. ans = max(ans, s[i]);
  51. }
  52. cout << ans << endl;
  53. }
  54. main() {
  55. bst;
  56. int t = 1;
  57. //cin >> t;
  58. while(t--) {
  59. solve();
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement