Pagini recente » Cod sursa (job #2584812) | Cod sursa (job #2252251) | Cod sursa (job #2266473) | Cod sursa (job #2490835) | Cod sursa (job #953439)
Cod sursa(job #953439)
#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)
{
MaxProfit = max(MaxProfit, 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;
}