Header Ad

HackerEarth Profits by cars problem solution

In this HackerEarth Profits by cars problem solution, You are a car seller. You have N cars and the profit for each of the cars is given by an array P. The profit of cars are P1, P2, P3, ..., PN. Since you got a huge profit in the last month so you decide to get (N - 1) more sets of such cars. You already have one car. Now, you have N^2 cars. Basically, there are N number of cars of each profit such as N cars for profit P1, N cars of profit P2, and so on up to N cars of profit PN.

You can perform the following operations any number of times:

If the last car is sold for profit P, then you can sell a car for profit Pc > P.

Note: You can select a car of any profit in the first operation as there are no cars that are sold earlier.

Find out the maximum profit that you can make.

For example, N = 4 and prices are P1, P2, P3, P4. Since N is 4, therefore you can have four sets of cars and the prices are P1, P2, P3, P4, P1, P2, P3, P4, P1, P2, P3, P4, P1, P2, P3, P4.


HackerEarth Profits by cars problem solution


HackerEarth Profits by cars problem solution.

#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007
#define test ll t; cin>>t; while(t--)
typedef long long int ll;
int main() {
FIO;
//test
{
ll n;
cin>>n;
ll x; set<ll>s;
for(int i=0;i<n;i++){
cin>>x;
s.insert(x);
}
ll ans=0;
for(auto it:s){
ans+=it;
}
cout<<ans<<endl;
}
return 0;
}

Second solution

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
ios::sync_with_stdio(0), cin.tie(0);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
n = unique(a, a + n) - a;
cout << accumulate(a, a + n, 0ll) << '\n';
}
}

Post a Comment

0 Comments