fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int Mod=998244353;
  5. const ll INF = -10000000000000;
  6.  
  7. void solve() {
  8. int n;
  9. cin >> n;
  10. vector<ll> a(n+1);
  11. for(int i = 1; i <= n; i++) cin >> a[i];
  12.  
  13. unordered_map<ll, vector<ll>> mp;
  14. for(int i = 2; i <= n; i++){
  15. ll need = a[i] + i - 1;
  16. ll gain = i - 1;
  17. mp[need].push_back(need + gain);
  18. }
  19.  
  20. ll best = n;
  21. unordered_set<ll> seen;
  22. queue<ll> q;
  23. q.push(n);
  24. seen.insert(n);
  25.  
  26. while(!q.empty()){
  27. ll L = q.front();
  28. q.pop();
  29. best = max(best, L);
  30.  
  31. auto it = mp.find(L);
  32. if(it == mp.end()) continue;
  33.  
  34. for(ll nxt : it->second){
  35. if(seen.insert(nxt).second){
  36. q.push(nxt);
  37. }
  38. }
  39. mp.erase(it);
  40. }
  41.  
  42. cout << best << "\n";
  43. }
  44.  
  45. int main(){
  46. ios::sync_with_stdio(false);
  47. cin.tie(nullptr);
  48.  
  49. int t;
  50. cin >> t;
  51. while (t--) solve();
  52.  
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0.01s 5296KB
stdin
4
5
2 4 6 2 5
5
5 4 4 5 1
4
6 8 2 3
1
1
stdout
10
11
10
1