I. Inverted Signs

I. Inverted Signs
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output

Arya lives in a magical world. You can view it as a number line.

There are N citizens in total. The i-th citizen have his own house in position i with height Hi, Noted that Hi could be non positive, which means their house is actually built beneath the horizon.

Arya is the hand of the king and she thinks that the buildings are a bit out of order. She denote the chaos index of the world as .

Noted that hi could be equal to zero as the world is magical.

She feels that the current chaos index of the world is too high. So she designs to change the world a bit by using her super power.

She could flip a continuous sequence of building, i.e., she could choose two arbitrary integer L and R, where 1 ≤ L ≤ R ≤ N, and invert the signs of Hi for all L ≤ i ≤ R (positive to negative and vice versa).

As Arya is weak, she would only do this operation exactly once. Arya wants to minimize the chaos index after the operation. Being a good guy, help Arya to find the lowest possible chaos index after exactly flipping one continuous sequence of building.

Input

The first line contains an integer N. (1 ≤ N ≤ 106)

The second line consist of N integers, the i-th integer is Hi. ( - 109 ≤ Hi ≤ 109)

Output

Output consist only one integer in a single line, the lowest possible chaos index after exactly flipping one continuous sequence of building.

Examples
input
Copy
4
1 -2 -3 4
output
3
input
Copy
5
-3 -2 0 -5 3
output
10
Note

In the first sample, Arya should flip house 2 to 3, so that the heights become 1, 2, 3, 4 and the chaos index would be |2 - 1| + |3 - 2| + |4 - 3| = 3.

|x - y| is the absolute difference between x and y.

题目解析:

这个题目的意思很简单就是让我们可以调整一个区间的数变成其相反数,然后求每两个相邻数之间和的最小值,很简答

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+1e2;
typedef long long LL;
LL a[maxn];
LL ans[maxn];
int main()
{
    //freopen("in.txt","r",stdin);
    int n;
    while(~scanf("%d",&n))
    {
        memset(a,0,sizeof(a));
        memset(ans,0,sizeof(ans));
        LL sum = 0;
        int cnt = 0;
        scanf("%lld",&a[0]);
        for(int i=1; i<n; i++)
        {
            scanf("%lld",&a[i]);
            sum+=abs(a[i]-a[i-1]);
            ans[cnt++] = abs(-a[i]-a[i-1])-abs(a[i]-a[i-1]);
        }
        sort(ans,ans+cnt);
        if(ans[0]<0)
            sum=sum+ans[0];
        if(ans[1]<0)
            sum+=ans[1];
        printf("%lld\n",sum);
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值