Pagini recente » Cod sursa (job #831951) | Cod sursa (job #2885539) | Cod sursa (job #424352) | Cod sursa (job #3254545) | Cod sursa (job #585960)
Cod sursa(job #585960)
# include <fstream>
# include <algorithm>
using namespace std;
std :: ifstream f ("avioane.in");
std :: ofstream g ("avioane.out");
int heap[100010][2];
int n, v[100010], x[100010];
int i, lung, sol;
inline int son1 (int a){
return a << 1;
}
inline int son2 (int a){
return (a << 1) + 1;
}
inline int dad (int a){
return a >> 1;
}
void swap (int &a, int &b){
int x = a;
a = b;
b = x;
}
void down_heap (int nr, int n){
int poz = nr, ini;
while (poz < lung){
ini = poz;
if (heap[son1 (poz)][0] * (n - heap[son1 (poz)][1] + 1) > heap[son2 (poz)][0] * (n - heap[son2 (poz)][1] + 1)){
if (heap[poz][0] * (n - heap[poz][1] + 1) < heap[son1 (poz)][0] * (n - heap[son1 (poz)][1] + 1)){
swap (heap[poz][0], heap[son1 (poz)][0]);
swap (heap[poz][1], heap[son1 (poz)][1]);
poz = son1 (poz);
}
}
else{
if (heap[poz][0] * (n - heap[poz][1] + 1) < heap[son2 (poz)][0] * (n - heap[son2 (poz)][1] + 1)){
swap (heap[poz][0], heap[son2 (poz)][0]);
swap (heap[poz][1], heap[son2 (poz)][1]);
poz = son2 (poz);
}
}
if (poz = ini) break ;
}
}
void up_heap (int nr, int n){
int poz = nr;
while (poz > 1){
if (heap[poz][0] * (n - heap[poz][1] + 1) > heap[dad (poz)][0] * (n - heap[dad (poz)][1] + 1)){
swap (heap[poz][0], heap[dad (poz)][0]);
swap (heap[poz][1], heap[dad (poz)][1]);
poz = dad (poz);
}
else break ;
}
}
int main (){
f >> n;
for (i = 1; i <= n; ++i) f >> v[i];
sort (v + 1, v + n + 1);
for (i = 1; i <= n; ++i){
heap[++lung][0] = v[i];
heap[lung][1] = i;
down_heap (1, i);
up_heap (lung, i);
x[i] = heap[1][0] * (i - heap[1][1] + 1);
}
for (i = n; i >= 1; --i){
if (sol < (n - i + 1) * v[i] + x[i - 1])
sol = (n - i + 1) * v[i] + x[i - 1];
}
g << sol << '\n';
g.close ();
return 0;
}