Pagini recente » Cod sursa (job #1004393) | Cod sursa (job #660440) | Cod sursa (job #1675248) | Cod sursa (job #1422902) | Cod sursa (job #953443)
Cod sursa(job #953443)
#include <fstream>
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#define MAXN 100001
using namespace std;
struct Profit
{
Profit() :
Price(0),
TotalProfit(0)
{}
int Price;
long long TotalProfit;
};
Profit profits[MAXN];
int main()
{
int n;
vector<int> vCustomers;
fstream fin("avioane.in", fstream::in);
fstream fout("avioane.out", fstream::out);
fin >> n;
//cout << n << endl;
istream_iterator<int> itIn(fin);
copy_n(itIn, n, back_inserter(vCustomers));
sort(vCustomers.begin(), vCustomers.end());
/*ostream_iterator<int> itOut(cout, " ");
copy_n(vCustomers.begin(), n, itOut);
cout << endl << endl;*/
long long MaxProfit = 0;
int ChosenPrice = 0;
for (int i=0; i<n; ++i)
{
if (vCustomers[i] != ChosenPrice)
{
int steps = MaxProfit / (vCustomers[i] - ChosenPrice);
if (i + steps < n)
{
profits[i + steps].TotalProfit = static_cast<long long>(vCustomers[i]) * (steps + 1);
profits[i + steps].Price = vCustomers[i];
}
MaxProfit += ChosenPrice;
if (MaxProfit <= profits[i].TotalProfit)
{
MaxProfit = profits[i].TotalProfit;
ChosenPrice = profits[i].Price;
}
profits[i].TotalProfit = MaxProfit;
profits[i].Price = ChosenPrice;
}
else
{
MaxProfit += ChosenPrice;
profits[i].TotalProfit = MaxProfit;
}
}
MaxProfit = 0;
for (int i=n-1; i>0; --i)
{
/*long long economyProfit = 0;
for (int j=i-1; j>=0; --j)
{
economyProfit = max(economyProfit, static_cast<long long>(vCustomers[j]) * (i - j));
}
MaxProfit = max(MaxProfit, static_cast<long long>(vCustomers[i]) * (n - i) + economyProfit);*/
MaxProfit = max(MaxProfit, static_cast<long long>(vCustomers[i]) * (n - i) + profits[i-1].TotalProfit);
}
/*for (int i=0; i<n; ++i)
{
cout << "(" << profits[i].TotalProfit << " " << profits[i].Price << ") ";
}
cout << endl;*/
fout << MaxProfit << "\n";
return 0;
}