Pagini recente » Cod sursa (job #2574930) | Cod sursa (job #636993) | Cod sursa (job #3272189) | Cod sursa (job #1041374) | Cod sursa (job #585920)
Cod sursa(job #585920)
#include <iostream>
#include <cstdio>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <algorithm>
using namespace std;
int N;
int V[100001];
typedef long long _ll;
_ll xV[100001];
static inline _ll maxBizProfit(int nEco, _ll ecoCost) {
_ll best = 0;
/*
static _ll prevEcoCost = 0;
_ll delta = ecoCost-prevEcoCost;
prevEcoCost = ecoCost;
*/
for (int nBiz = 0; nBiz <= nEco; nBiz++) {
_ll bizProfit = xV[nBiz] - ecoCost*nBiz;
if (bizProfit > best) best = bizProfit;
}
return best;
}
static inline _ll maxBizProfitCheck(int nEco, _ll ecoCost) {
_ll best = 0;
for (int nBiz = nEco; nBiz >= 1; nBiz--) {
_ll bizCost = V[nBiz-1];
_ll bizProfit = (bizCost-ecoCost) * (_ll)nBiz;
if (bizProfit > best) {
best = bizProfit;
}
}
return best;
}
int main(int argc, char** argv) {
FILE* fIn = fopen("avioane.in", "rt");
ofstream fOut("avioane.out");
fscanf(fIn, "%d", &N);
for (int i=0; i<N; i++) fscanf(fIn, "%d", V+i);
//
/*
srand(1234);
N = 1000;
for (int i=0; i<N; i++) V[i] = rand() % 1000;
*/
// 335162
sort(V, V+N);
for (int i=0; i<N/2; i++) {
int temp = V[i];
V[i] = V[N-1-i];
V[N-1-i] = temp;
}
xV[0] = 0;
for (int i=1; i<=N; i++) {
xV[i] = ((_ll)V[i-1]) * i;
}
_ll best = 0;
for (int nEco = N; nEco >= 1; nEco--) {
_ll ecoCost = V[nEco-1];
_ll ecoProfit = ecoCost * nEco;
_ll bizProfit = maxBizProfit(nEco, ecoCost);
/*
if (bizProfit != maxBizProfitCheck(nEco, ecoCost)) {
cout << "ERROR\n";
exit(1);
}
*/
if (ecoProfit+bizProfit > best) best = ecoProfit+bizProfit;
}
fOut << best << "\n";
fOut.close();
}