Pagini recente » Autentificare | Cod sursa (job #224788) | Cod sursa (job #2034404) | Cod sursa (job #1503714) | Cod sursa (job #2626564)
#define MAX_N 100000
#define INF 0x3f3f3f3f
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("avioane.in");
ofstream fout("avioane.out");
int n, B[MAX_N + 1];
int DP[MAX_N + 1];
int l, r;
int indMa;
int step;
int profit;
int ProfitFunc(int left, int right);
int OutrunStep(int left, int right);
int StepProfit(int index);
void Insert();
int main()
{
fin >> n;
for (int i = 1; i <= n; ++i)
{
fin >> B[i];
}
sort(B + 1, B + 1 + n);
l = 1;
r = 0;
indMa = 1;
profit = 0;
for (step = 2; step <= n; ++step)
{
if (l <= r)
{
if (OutrunStep(indMa, DP[l]) == step)
{
indMa = DP[l];
++l;
}
}
{
int aux = ProfitFunc(indMa, step);
if (profit < aux)
{
profit = aux;
}
}
Insert();
}
fout << profit;
fin.close();
fout.close();
return 0;
}
int ProfitFunc(int left, int right)
{
return (B[left] * (right - left)) + (B[right] * (n - right + 1));
}
int OutrunStep(int left, int right)
{
if (B[left] == B[right])
{
return INF;
}
int vel = B[right] - B[left];
int dist = B[left] * (right - left + 1) - B[right];
if (dist <= 0)
{
return 0;
}
int res = dist / vel;
if ((dist % vel) != 0)
{
++res;
}
return res + right;
}
int StepProfit(int index)
{
return B[index] * (step - index + 1);
}
void Insert()
{
if (StepProfit(indMa) <= StepProfit(step))
{
indMa = step;
l = 1;
r = 0;
return;
}
if (OutrunStep(DP[r - 1], DP[r]) < OutrunStep(DP[r - 1], step))
{
DP[++r] = step;
return;
}
int pos = -1;
int left = l, right = r, mid;
while (left <= right)
{
mid = (left + right) / 2;
if (OutrunStep(DP[mid - 1], DP[mid]) >= OutrunStep(DP[mid - 1], step))
{
pos = mid;
right = mid - 1;
}
else
{
left = mid + 1;
}
}
if (pos == -1)
{
l = r = 1;
DP[1] = step;
}
else
{
r = pos;
DP[pos] = step;
}
}