CodeForces 1084A The Fair Nut and Elevator 题解

A. The Fair Nut and Elevator

time limit per test : 1 second

memory limit per test : 256 megabytes

input : standard input

output : standard output

The Fair Nut lives in ?? story house. ???? people live on the ??-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 ??-th floor, but ?? hasn‘t been chosen yet. When a person needs to get from floor ?? to floor ??, elevator follows the simple algorithm: Moves from the ??-th floor (initially it stays on the ??-th floor) to the ??-th and takes the passenger.Moves from the ??-th floor to the ??-th floor and lets out the passenger (if ?? equals ??, elevator just opens and closes the doors, but still comes to the floor from the ??-th floor).Moves from the ??-th floor back to the ??-th. The elevator never transposes more than one person and always goes back to the floor ?? before transposing a next passenger. The elevator spends one unit of electricity to move between neighboring floors. So moving from the ??-th floor to the ??-th floor requires |?????| 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 ??-th floor. Don‘t forget than elevator initially stays on the ??-th floor.

Input

The first line contains one integer ?? (1≤??≤100) — the number of floors.

The second line contains ?? integers ??1,??2,…,???? (0≤????≤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

3 0 2 1

Output

16

Input

2

1 1

Output

4

Note

In the first example, the answer can be achieved by choosing the second floor as the ??-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 ??-th floor.

思路

枚举答案,在进行验算即可。

代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#define N 1001
using namespace std;
long long a[N],n,ans=0x7fffffff;
long long _abs(long long k)
{
    if(k>=0) return k;
    else return (0-k);
}
long long f1(long long x,long long k)
{

    long long ret=_abs(x-k)+k+x-2;
    ret*=(2*a[k]);
    return ret;
}
int main()
{
    scanf("%lld",&n);
    for(long long i=1;i<=n;i++) scanf("%lld",&a[i]);
    for(long long i=1;i<=n;i++)
    {
        long long ls=0;
        for(long long j=1;j<=n;j++) ls+=f1(i,j);
        if(ls<ans) ans=ls;
    }
    printf("%lld\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/zj-mrz/p/10198977.html

时间: 2024-08-06 14:21:07

CodeForces 1084A The Fair Nut and Elevator 题解的相关文章

CodeForces 1084D The Fair Nut and the Best Path

The Fair Nut and the Best Path 题意:求路径上的 点权和 - 边权和 最大, 然后不能存在某个点为负数. 题解: dfs一遍, 求所有儿子走到这个点的最大值和次大值. 我们需要明白如果可以从u -> v  那么一定可以从 v -> u, 当然 指的是 u->v是路径上的最大和. u->e1->v; 假如:val[u] = 100, val[e1] = 50, val[v] = 60, 那么我们发现可以从 u -> v 也可以从v ->

Codeforces Round #526 (Div. 2) C. The Fair Nut and String

C. The Fair Nut and String 题目链接:https://codeforces.com/contest/1084/problem/C 题意: 给出一个字符串,找出都为a的子序列(比如ai,aj,ak)满足以下条件的个数: 1.子序列的索引单增(i<j<k): 2.在原字符串中,若ai=aj=ak=a,那么满足i<=k1<j,j<=k2<k 并且 ak1=ak2=b. 通俗点说,就是找这样的子序列个数:要么单个a,要么每个a之间都有一个b. 题解:

Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path

D. The Fair Nut and the Best Path 题目链接:https://codeforces.com/contest/1084/problem/D 题意: 给出一棵树,走不重复的路径,每到一个结点加上其权值,经过一条边减去其权值,路径中途减去后不能出现负数,问怎么选择路径可以让最后得到的最大. 题解: 这题考虑用dp来做. 我们定义dp[u]为走到u点的最大值,注意这里的方向,是走到u点.题目中的意思是路径不能走回头路. 对于一个父节点u,那么我们可以根据走到其儿子结点的最

Codeforces Round #526 (Div. 2) E. The Fair Nut and Strings

E. The Fair Nut and Strings 题目链接:https://codeforces.com/contest/1084/problem/E 题意: 输入n,k,k代表一共有长度为n的多少个字符串.然后给出一个最小字符串s,最大字符串t,满足对于所有的k个字符串有s<=S<=t. 最后求满足条件的k个字符串(自己构造)的不同前缀数量的和. 题解: 这题很巧妙,设dp(i)表示长度为i的前缀的数量和,一开始有dp(1)=0. 后来随着长度的增加,我们每次可以在最后加一个a或者b,

codeforces 897A Scarborough Fair 暴力签到

codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: #include <iostream> #include <stdio.h> #include <string.h> using namespace std; typedef long long ll; int n,m; string s; int main() { ios

Codeforces Round #526 C - The Fair Nut and String /// 组合递推

题目大意: 给定原字符序列 找出其中所有子序列满足 1.序列内字符都为a 2.若有两个以上的字符 则相邻两个字符在原序列中两者之间存在字符b 的数量 将整个字符序列用b分开 此时再得到每个b之间a的数量 即 abbgaaba 得到 v[] = { 1 0 2 1 } 此时假设到第 i-1 段 已得到在第 i-1 段内的所有方案数为 ans (长度为1.2.3.... .i-1) 则在第 i 段时 可由前一段的方案数 和 当前段数量 组合得到ans*v[ i ] (长度为2.3.4.... .i)

Codeforces 821A Okabe and Future Gadget Laboratory 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://codeforces.com/problemset/problem/821/A 时间限制:2秒 空间限制:256M Okabe要改进他的实验室.实验室用一个n*n的正方形网格表示(n为正整数).他认为,一个“好实验室”的网格内每一个不等于1的数字都可以用同一行和同一列的某个数字之和表示.换句话说,对于任意x,y(1 ≤ x, y ≤ n 且 ax, y ≠ 1,),存在两个数s和t,使得ax, y = 

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces #282 div 1 C Helping People 题解

CF 282 C Helping People 题解 [原题]不贴了. [废话]好久没写博客了.(我不会告诉你我是离线写的)于是来水经验来了. [来源简述]CF 282 C [原题简述]有N(10^5)个人,每个人有初始的钱.再给出M(5000)个操作L,R,P.每次表示L~R这些人有几率P(0<=P<=1)给他们每人一元.求最后所有人钱数最大值的期望. [算法简述]首先把这些操作建立出树结构(可以借鉴线段树).节点i表示范围Li~Ri,它的父亲一定包含它,它也包含它的所有子树.为了方便,建立