I am solving this question
but i am getting accepted in task 0 only and wrong verdict in other cases
Logic
suppose we have 4 elements
3 10 3 5
here first team will play with 2,3,4 teams
so total revenue will be the absolute difference between the strengths of two teams playing the matches .
so i took suffix sum
21 18 8 5
now as first team will play with next 3 teams suppose we want revenue zero then among all others teams each team should have score 3 and so 9 for 3 teams but from suffix sum
tells that score by next 3 teams is 18 .
In this revenue will be abs(18-9) == 9
same for others
This works in O(N) .
Can anyone tell me why i am getting wrong ans ??
Here’s is my code :
vi a , suffix ;
void Go () {
int n = 0 ;
cin >> n ;
a.assign(n,0) ;
suffix.assign(n,0) ;
for ( int i = 0 ; i < n ; i++ ) {
cin >> a[i] ;
suffix[i] = a[i] ;
}
for ( int i = n-2 ; i >= 0 ; i-- ) {
suffix[i] += suffix[i+1] ;
}
int ans = 0 ;
for ( int i = 0 ; i < n-1 ; i++ ) {
int expected = a[i]*(n-i-1) ;
int have = suffix[i+1] ;
ans += abs(have-expected) ;
}
cout << ans << Endl ;
}