## playboy307

Java，golang后台开发，欢迎互相交流学习

# CodeForces ---596B--Wilbur and Array(贪心模拟)

 Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Description

Wilbur the pig is tinkering with arrays again. He has the array a1, a2, ..., an initially consisting of n zeros. At one step, he can choose any index i and either add 1 to all elements ai, ai + 1, ... , an or subtract 1 from all elements ai, ai + 1, ..., an. His goal is to end up with the array b1, b2, ..., bn.

Of course, Wilbur wants to achieve this goal in the minimum number of steps and asks you to compute this value.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array ai. Initially ai = 0 for every position i, so this array is not given in the input.

The second line of the input contains n integers b1, b2, ..., bn ( - 109 ≤ bi ≤ 109).

Output

Print the minimum number of steps that Wilbur needs to make in order to achieve ai = bi for all i.

Sample Input

Input
5
1 2 3 4 5

Output
5
Input
4
1 2 2 1

Output
3

Hint

In the first sample, Wilbur may successively choose indices 1, 2, 3, 4, and 5, and add 1 to corresponding suffixes.

In the second sample, Wilbur first chooses indices 1 and 2 and adds 1 to corresponding suffixes, then he chooses index 4 and subtract 1.

Source

Codeforces Round #331 (Div. 2)

#include<stdio.h>
#include<string.h>
#include<math.h>
int num[200000+10];
int main()
{
int n;
long long sum=0;//数据范围略大，操作次数多了点
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
sum+=abs(num[i]-num[i-1]);
}
printf("%lld\n",sum);
return 0;
}

#### Codeforces 733C （模拟，贪心）

2016-11-01 16:53:20

#### Codeforces 796C Bank Hacking 贪心+规律

2017-04-11 04:45:50

#### CodeForces - 808C-贪心-思维

2017-05-21 19:50:54

#### Codeforces 854 C Planning（优先队列+贪心）

2017-09-06 23:41:56

#### Codeforces 3 B. Lorry 暴力 排序 枚举 贪心

2014-10-18 09:03:20

#### CodeForces 538B Quasi Binary(不错的贪心题 进制想法)

2015-05-05 19:41:36

#### Codeforces 798D Mike and distribution（二维贪心）

2017-04-22 22:45:47

#### Codeforces 706d 字典树+贪心

2016-08-13 23:36:26

#### CodeForces 140C 贪心+优先队列

2016-11-25 21:39:12