This has a time limit of one second. My program takes 1.01s for some unknown test cases. The problem is
You are developing a smartphone app. You have a list of potential customers for your app. Each customer has a budget and will buy the app at your declared price if and only if the price is less than or equal to the customer's budget.
You want to fix a price so that the revenue you earn from the app is maximized. Find this maximum possible revenue.
For instance, suppose you have 4 potential customers and their budgets are 30, 20, 53 and 14. In this case, the maximum revenue you can get is 60.
I'm sure that the code is correct. I just want to optimize it. Please help me do so. I'm a beginner.
Code:
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int n, x, i, j, k;
cin >> n;
vector<int> v;
for (i = 0; i < n; ++i)
{
cin >> x;
v.push_back(x);
}
int maximum = *max_element(v.begin(), v.end());
int minimum = *min_element(v.begin(), v.end());
vector<long long int> sums;
for (j = minimum; j <= maximum; ++j)
{
vector<int> prices;
for (k = 0; k < n; ++k)
{
if (j > v[k]) {
;
} else {
prices.push_back(j);
}
}
sums.push_back(accumulate(prices.begin(), prices.end(), 0));
prices.clear();
}
long long int maxsum = *max_element(sums.begin(), sums.end());
cout << maxsum;
}