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