Description
需要把不同機房的n個設(shè)備開機,一開始在電梯里,對每個設(shè)備,都需要移動到設(shè)備旁開機,然后回到電梯。
已知從電梯走到每個設(shè)備所需的時間(返回時間相同)。從 0 時刻起,到每個設(shè)備被開機的時間為該設(shè)備等待時間。求所有設(shè)備最小的總等待時間。電梯移動的時間忽略不計。
Input
每組數(shù)據(jù)第一行為設(shè)備數(shù) 1?≤?n?≤?1000,接下來 n 行每行一個距離 1?≤?di?≤?1000 表示電梯走到第 i 個設(shè)備的時間。
Output
所有設(shè)備最小的總等待時間。
Sample Input
3
2
1
5
Sample Output
16
Hint
先啟動第 2 個設(shè)備,電梯走到該設(shè)備耗時 1,當(dāng)前時間點為 1,該設(shè)備等待時間為 1
走回電梯,耗時 1,走到第 1 個設(shè)備耗時 2,當(dāng)前時間點為 4,該設(shè)備等待時間為 4
走回電梯,耗時 2,走到第 3 個設(shè)備耗時 5,當(dāng)前時間點為 11,該設(shè)備等待時間為 11
總耗時為 1+4+11=16
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int n, d[1100];
int main()
{
while(scanf("%d", &n) != EOF)
{
for(int i = 0; i < n; i ++)
scanf("%d", &d[i]);
sort(d, d + n);
int delay = 0;
long long ans = 0;
for(int i = 0; i < n; i ++)
{
delay += d[i];
ans += delay;
delay += d[i];
}
printf("%lld\n", ans);
}
return 0;
}