PROBLEM SETTER  DIFFICULTY  PREREQUISITES  KEY OBSERVATIONS  FIRST SUBTASK SOLUTION  int ans=0; for(int i = 1;i < n+1;i++) { int cnt=0; for(int j = 1;j < n+1;j++) if(a[j] <= a[i]) cnt++; ans = max(ans,cnt*a[i]); } cout << ans; SECOND SUBTASK SOLUTION  sort(a+1,a+n+1); ll ans=0,cnt=0; for(ll i = 1;i < n+1;i++) { cnt = a[i]*(ni+1); ans = max(ans,cnt); } cout << ans; This works because, after sorting, the checking process that was initially happening in O(N) for each index will now happen in O(1) time as the number of elements bigger than the $i^{th}$ element is already known to be (Ni+1). Don't forget to store answer in long long format. TIME COMPLEXITY 
This question is marked "community wiki".
asked 23 Jun '17, 15:34

This code does literally the same thing, still getting WA on most TCs. Please help. https://www.codechef.com/viewsolution/21759944 answered 06 Dec, 20:47
