Codeforces Round #526 (Div. 2) A
3208 단어 훈련경기
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The Fair Nut lives in n story house. ai people live on the i
-th floor of the house. Every person uses elevator twice a day: to get from the floor where he/she lives to the ground (first) floor and to get from the first floor to the floor where he/she lives, when he/she comes back home in the evening.
It was decided that elevator, when it is not used, will stay on the x -th floor, but x hasn't been chosen yet. When a person needs to get from floor a to floor b
, elevator follows the simple algorithm:
-th floor (initially it stays on the x-th floor) to the a
The elevator never transposes more than one person and always goes back to the floor x before transposing a next passenger. The elevator spends one unit of electricity to move between neighboring floors. So moving from the a-th floor to the b-th floor requires |a−b| units of electricity. Your task is to help Nut to find the minimum number of electricity units, that it would be enough for one day, by choosing an optimal the x -th floor. Don't forget than elevator initially stays on the x -th floor. Input The first line contains one integer n (1≤n≤100 ) — the number of floors. The second line contains n integers a1,a2,…,an (0≤ai≤100 ) — the number of people on each floor. Output In a single line, print the answer to the problem — the minimum number of electricity units. Examples Input Copy
3
0 2 1
Output Copy 16
Input Copy 2
1 1
Output Copy 4
Note In the first example, the answer can be achieved by choosing the second floor as the x -th floor. Each person from the second floor (there are two of them) would spend 4 units of electricity per day (2 to get down and 2 to get up), and one person from the third would spend 8 units of electricity per day (4 to get down and 4 to get up). 4⋅2+8⋅1=16 . In the second example, the answer can be achieved by choosing the first floor as the x -th floor. 제목: 이 문제는 엘리베이터의 가장 좋은 층을 구하는 것이다. 매번 사람을 맞이할 때 엘리베이터는 가장 좋은 층에서 사람이 있는 층으로 가서 찾은 층으로 보내고 다시 가장 좋은 층으로 돌아간다. 한 층으로 갈 때마다 1의 전력을 소모하고 총 전력을 최소화해야 한다.
문제풀이: 그냥 공식적으로 쓰면 돼. 3층에서 1층까지 2개의 전기가 소모된다는 걸 기억해!
#include
using namespace std;
const int maxn = 105;
int num[maxn];
int main() {
int n,ans=9999999;
cin >>n;
for(int i = 1; i <= n; i++) {
scanf("%d",&num[i]);
}
for(int i = 1;i <= n;i++){
int temp = 0;
for(int j = 1;j <= n;j++){
temp += 2 * num[j] * (abs(i-j) + abs(j-1) + abs(1-i));
}
ans = min(temp,ans);
}
cout << ans << endl;
}