B. Wilbur and Array

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/596/problem/B

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

5
1 2 3 4 5

Sample Output

5

HINT

题意

a[i]数组一开始都是0

然后你可以使得i-n都加1,也可以使得i-n都减一

然后问你最少操作数

题解:

扫一遍就好了,直接暴力,注意会爆int

代码

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define maxn 200005
long long b[maxn];
int main()
{
int n;cin>>n;
for(int i=;i<n;i++)
scanf("%lld",&b[i]);
long long now = ;
long long ans = ;
for(int i=;i<n;i++)
{
if(now!=b[i])
{
ans+= abs(b[i]-now);
now = b[i];
}
}
printf("%lld\n",ans);
}

Codeforces Round #331 (Div. 2) B. Wilbur and Array 水题的更多相关文章

  1. Codeforces Round #331 (Div. 2) B. Wilbur and Array

    B. Wilbur and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题

    Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  3. Codeforces Round #331 (Div. 2) _A. Wilbur and Swimming Pool

    A. Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megabytes input s ...

  4. Codeforces Round #290 (Div. 2) A. Fox And Snake 水题

    A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...

  5. Codeforces Round #322 (Div. 2) A. Vasya the Hipster 水题

    A. Vasya the Hipster Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/p ...

  6. Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题

    B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...

  7. Codeforces Round #368 (Div. 2) A. Brain's Photos 水题

    A. Brain's Photos 题目连接: http://www.codeforces.com/contest/707/problem/A Description Small, but very ...

  8. Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题

    A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...

  9. Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题

    A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...

随机推荐

  1. Ruby窗口程序

    require 'tk' tkroot=TkRoot.new { title 'hellw word' geometry '300x200' } lb = TkLabel.new(tkroot) do ...

  2. Zend Framework 入门(4)—页面布局

    Zend Framework 的页面布局模块——Zend_Layout——既可以跟 MVC 一起使用,也可以单独使用.本文只讨论与 MVC 一起使用的情况. 1. 布局脚本 在 application ...

  3. [转] AE之分级颜色专题图渲染

    原文 AE之分级颜色专题图渲染 参考代码1 private void 分级渲染ToolStripMenuItem_Click(object sender, EventArgs e) { //值分级 I ...

  4. Web自动化框架搭建之二基于数据驱动应用简单实例~~

    整体框架,先划分成细小功能模块~~,从最简单的开始,介绍 实现循环百度搜索实例: #coding=utf-8 '''Created on 2014��6��9�� @author: 小鱼'''impo ...

  5. List转换成Json、对象集合转换Json等

    #region List转换成Json /// <summary> /// List转换成Json /// </summary> public static string Li ...

  6. ASP.NET开发在JavaScript有中文汉字时出现乱码时简单有效的解决

    一般情况在使用ASP.NET开发使用JavaScript有中文汉字时不会出现乱码情况,比如:alert('您看到我了吗?');这样直接输入中文汉字的代码中是不会出现乱码的,如果出现了,一是检查Web. ...

  7. 利用原生JavaScript获取样式的方式小结

    来源:http://www.ido321.com/930.html ps:是获取样式,不是设置样式.若没有给元素设置样式值,则返回浏览器给予的默认值.(论坛整理) 1.element.style:只能 ...

  8. CSS_网站配色参考方案

    http://www.cnblogs.com/QLeelulu/archive/2008/04/04/1136974.html   Shiny silver [#EEEEEE]       Reddi ...

  9. String比较

    String str1 = "abc"; String str2 = "abc"; String str3 = new String("abc&quo ...

  10. windows防火墙无法启动,服务不存在

    系统的防火墙无法启动,系统服务里面也没有防火墙的服务,用下面第一种方法,导 入注册表,重启服务器,在服务里面有防火墙的服务了,然后启动就好了,需要重新配置防火墙 在控制面板中启动防火墙的时候提示无法识 ...