Pagini recente » Cod sursa (job #706012) | Cod sursa (job #2804318) | Cod sursa (job #609756) | Cod sursa (job #108471) | Cod sursa (job #2239052)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const long double INF = 1e9;
struct dreapta
{
//a*x + b
dreapta(long long a = 0, long long b = 0, long long ind = -1)
{
this->a = a;
this->b = b;
this->ind = ind;
}
long long a, b, ind;
};
inline long double IntersectX(const dreapta &x, const dreapta &y)
{
return 1.0 * (y.b - x.b) / (x.a - y.a);
}
int main()
{
ifstream in("avioane.in");
int n;
in >> n;
vector<long long> v(n);
for(auto &x:v)
in >> x;
in.close();
sort(v.begin(), v.end());
vector<pair<long double, dreapta> > st;
dreapta dr;
int j = 0;
long long rasp = 0;
for(int i = 0; i < n; ++i)
{
while(j+1 < st.size() && st[j+1].first < i)
j++;
if(i > 0)
{
int ind = st[j].second.ind;
rasp = max(rasp, 1LL * (n - i) * v[i] + 1LL * (i - ind) * v[ind]);
}
dr = dreapta(v[i], (n - i) * v[i] - n * v[i], i);
while(st.size() > 1 && ( (dr.a == st.back().second.a && dr.b >= st.back().second.b) || IntersectX(dr, st.back().second) < st.back().first) )
{
st.pop_back();
}
if(st.empty())
st.push_back(make_pair(-INF, dr));
else
{
if(dr.a == st.back().second.a && dr.b < st.back().second.b)
continue;
st.push_back(make_pair(IntersectX(st.back().second, dr), dr));
}
}
ofstream out("avioane.out");
out << rasp;
out.close();
return 0;
}