 # ZCO 2014, Can anyone tell me the algorithm for this problem? Mine is working wrong.

The problem statement is: http://www.iarcs.org.in/inoi/2014/zco2014/zco2014-2a.php

Here is my code:
My logic is that if we sort the budgets and find the maximum value of i-th budget times (n-i). (0-based index). n-i means the number of people having budget greater than or equal to ith-budget.

``````#include<iostream>
#include<fstream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<queue>
#include<stack>
#include<cmath>
#include<utility>
using namespace std;

int main() {
int n;
cin >> n;
int a[n];
for(int i = 0;i < n;i++) cin >> a*;
sort(a,a+n);
long long int ans = 0;
for(int i = 0;i < n;i++) {
ans = (ans> ((n-i)*a*) )?ans:(n-i)*a*;
}
cout << ans;
return 0;
}
``````
1 Like

Here’s my idea on how to find the highest revenue, although I’m not sure if it is correct.

I think the best price would be the median of the array. For the odd length arrays, you can use the middle element and calculate the revenue generated. For even length arrays, you can calculate the revenues for both the elements in the middle and use the greater one.

1 Like

Your logic is correct use long long instead of int in everything except main().

1 Like

You declare ans as an array of size n and store (n-i)a in ans*. Eventually sort the array (ans) and it’s last element is the answer. (I used this logic and had scored 100 upon 100 in the ZCO online practice server).

Hope it helps!

even in the loops

This is also written as a note and the end of problem!

1 Like

Thanks but why didn’t this code work? int < 10^9 and I think it should suffice the constraints. Maybe there’s something wrong in their judge.

The code doesn’t work because your multiplication in (n-i)a is a multiplication of 2 integers and will overflow int since, (n-i)a can be larger than 10^9.

2 Likes

ooooooooooooooooooooo…i was also hvin the same prob

1 Like