Untitled
// Online C++ compiler to run C++ program online #include <iostream> #include <bits/stdc++.h> using namespace std; int maxWeightCell(int n, vector<int> edge){ vector<int> cnt(n,0); for(int i=0; i<n; i++){ if(edge[i] != -1) cnt[edge[i]] += i; } int ans = -1; int maxi = INT_MIN; for(int i=0; i<n; i++){ if(maxi <= cnt[i]){ maxi = cnt[i]; ans = i; } } return ans; } int main() { int n; cin >> n; vector<int> edges(n); for (int i = 0; i < n; ++i) { cin >> edges[i]; } cout<<maxWeightCell(n, edges)<<endl; return 0; }
Leave a Comment