Pagini recente » Cod sursa (job #1296393) | Cod sursa (job #1741333) | Cod sursa (job #353792) | Cod sursa (job #758165) | Cod sursa (job #826964)
Cod sursa(job #826964)
#include <queue>
#include <vector>
#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;
const int NMAX = 100011;
deque< int > dQ;
vector< int > v;
inline long long int _max(long long int x, long long int y) {return x >= y ? x : y;}
inline bool intersect(int i, int x1, int x2) {return 1LL * v[x2] * (i - x2) >= 1LL * v[x1] * (i - x1);}
inline bool intersectFaster(int maxIndex, int x1, int x2)
{
if(maxIndex == x1)
{
return false;
}
return (1LL * v[x2] * x2 - 1LL * v[maxIndex] * maxIndex) / (v[x2] - v[maxIndex]) > (1LL * v[x1] * x1 - 1LL * v[maxIndex] * maxIndex) / (v[x1] - v[maxIndex]);
}
int main()
{
long long int profit;
int N, i, j, x;
ifstream in("avioane.in");
ofstream out("avioane.out");
in >> N;
profit = 0;
switch(N)
{
case 2 : in >> profit;
case 1 : in >> x;
profit += x;
out << profit << '\n';
return EXIT_SUCCESS;
default : copy(istream_iterator<int>(in), istream_iterator<int>(), back_inserter(v));
}
sort(v.begin(), v.end());
profit = N * v[0];
dQ.push_back(0);
for(i = 1; i < N; ++i)
{
while(dQ.size() > 1)
{
if(intersect(i, dQ[0], dQ[1])) dQ.pop_front();
else break;
}
for( j = dQ[0]; j < i; ++j)
profit = _max(profit, 1LL * v[i] * (N - i) + 1LL * v[j] * (i - j));
if(v[i] == v[i-1]) continue;
while(false == dQ.empty())
{
if(intersectFaster(dQ[0], dQ.back(), v[i])) dQ.pop_back();
else break;
}
dQ.push_back(i);
}
out << profit << '\n';
return EXIT_SUCCESS;
}