2013. Pay Back

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

"Never a borrower nor a lender be." O how Bessie wishes she had taken that advice! She has borrowed from or lent money to each of N (1 ≤ N ≤ 100,000) friends, conveniently labeled 1..N.

Payback day has finally come. She knows she is owed more money than she owes to the other cows. They have all lined up in a straight line, cow i standing i meters from the barn. Bessie is going to traverse the line collecting money from those who owe her and reimbursing money to those she owes.

As she moves down the line, she can request any cow who owes her money to give her the money. When she has enough money to pay off any or all of her debts, she can pay the (recently collected) money to those she owes. Cow i owes Bessie D_i money (-1,000 ≤ D_i ≤ 1,000; D_i != 0). A negative debt means that Bessie owes money to the cow instead of vice-versa.

Bessie starts at the barn, location 0. What is the minimum distance she must travel to collect her money and pay all those she owes? She must end her travels at the end of the line.

 

Input

Line 1: A single integer: N 
Lines 2..N+1: Line i+1 contains a single integer: D_i

Output

Line 1: A single integer that is the total metric distance Bessie must travel in order to collect or pay each cow.

Sample Input

5
100
-200
250
-200
200

Sample Output

9
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int N;
cin >> N;
int i;
vector<int> road(N);
for(i = 0;i < N;i++)
cin >> road[i];
int d = road[0];
int sum = 0;
int p;
for(i = 1;i < N;i++)
{
d += road[i];
if(d < 0 && d - road[i] >= 0)
p = i;
else if(d >= 0 && d - road[i] < 0)
sum += i + i - p - p;
}
sum += N;
cout << sum << endl;
return 0;
}

soj2013.Pay Back的更多相关文章

  1. Apple Pay 初探

    Apple Pay 一.概述 1.支付方式:Touch ID/ Passcode 2.设备要求:iPhone6以上(iphone:线上/线下 ipad:线上 watch:线下) 3.系统要求:iOS8 ...

  2. 【转】iOS开发 -- Apple Pay

    技术博客原地址:http://www.cnblogs.com/dashunzi/p/ApplePay.html#top 原技术博客中有源码和视频,有感兴趣的朋友可以研究一下! 一.什么是Apple P ...

  3. Apple Pay的快速实现

    一.在Apple开发者中心配置 AppleID 和 Merchant IDs 二.配置好证书后在Xcode中开启Apple Pay 三.代码实现 3.1 判断是否支持Apple Pay,如果支持又将支 ...

  4. 开发apple pay碰到的问题总结

    本来想简单总结一下Apple Pay 开发过程中的几个问题, 结果被下面这篇文章全碰上了, 干脆全文转载, 作者对相关资源整理得比较详细, 比较有参考价值 总的来说, 我们做过 APNs 推送的话, ...

  5. iOS Apple Pay

    iOS 苹果支付 需要证书支持支付功能 targets 打开支付功能按钮 //ApplePay#import <PassKit/PassKit.h>                     ...

  6. Apple Pay

    Apple Pay运行环境:iPhone6以上设备,操作系统最低iOS9.0以上,部分信息设置需要iOS9.2以上.目前还不支持企业证书添加. 环境搭建好后可以在模拟器上面运行,xcode7.2.1+ ...

  7. iOS开发 Apple Pay

    一.什么是Apple Pay? 1. 概念 Apple Pay,简单来说, 就是一种移动支付方式.通过Touch ID/ Passcode,用户可使用存储在iPhone 6, 6p等设备上的信用卡和借 ...

  8. “粪便银行”:救人拿钱两不误 A Poop Bank in Massachusetts Will Pay You $40 Every Day

    “粪便银行”:救人拿钱两不误 如果你年龄小于50岁,排便规律,而且愿意每天去美国麻省麦德福德跑一趟,那么没准你可以每天得到40美元的外快,而你需要做的事情只是生产便便. 要想得到这笔收入,请拜访“开放 ...

  9. Apple Pay(转)

    Apple Pay 是在 iOS 8 中第一次被介绍,它可以为你的应用中的实体商品和服务,提供简单.安全.私密的支付方式.它使得用户支付起来非常简便,只需按一下指纹就可以授权进行交易. Apple P ...

随机推荐

  1. 用虚拟机安装了一台Linux系统,突然想克隆一台服务器,克隆后发现无法上网,如何解决?

    用虚拟机安装了一台Linux系统,突然想克隆一台服务器,克隆后发现无法上网,如何解决? 答:    a.编辑网卡配置文件/etc/sysconfig/network-scripts/ifcfg-eth ...

  2. 虚拟机VMware中的CentOS字符界面和图形界面切换

    在虚拟机中安装CentOS后展示的界面是图形用户界面,想切换到命令模式,于是查了linux下切换模式的方法,可是按了ctrl+alt+f1.f2....f7都是没用,后来发现是因为按键冲突的原因 问题 ...

  3. java中的变量各占得字节数

    boolen,8位1个字节int,32位,4个字节float,32位 4个字节double,64位8个字节char 16位,2个字节byte 8位1个字节short 16位 2个字节long 64位 ...

  4. Git常用的几个命令

    标签(空格分隔): Git 在本地文件系统中新建目录,放置你的工程: mk dir parkk cd parkk //进入该目录 git init //初始化自己的仓库,默认名称为master 在仓库 ...

  5. MT【143】统一分母

    已知$a,b>0$,则$m=\dfrac{b^2+2}{a+b}+\dfrac{a^2}{ab+1}$的最小值是______ 解答: $$m\geqslant \dfrac{b^2+2}{\sq ...

  6. Swift中使用MPMoviePlayerController实现自定义视频播放器界面

    默认情况下播放器自带各种控制按钮,比如前进后退播放暂停等: var url = NSBundle.mainBundle().URLForResource("1", withExte ...

  7. BZOJ 3881: [Coci2015]Divljak

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 553  Solved: 176[Submit][Sta ...

  8. 【BZOJ4006】【JLOI2015】管道连接

    Description 传送门 Solution 题目要求相同颜色的点必须在一个连通块中,但会有多个颜色同属一个连通块使得解更优的情况. 想一想DP能否行得通:设\(g_i\)表示已考虑颜色状态为\( ...

  9. 【bzoj2741】[FOTILE模拟赛] L

    Portal --> bzoj2741 Solution 突然沉迷分块不能自拔 考虑用分块+可持久化trie来解决这个问题 对于每一块的块头\(L\),预处理\([L,i]\)区间内的所有子区间 ...

  10. Redis学习基础一

    今天开始系统的学习redis基础知识,以往只是看redis的手册,貌似总是记不住.这次尝试手记笔记,使得印象更加深刻,从零开始学习.看是很慢,其实很快哟. 一.什么是Redis 至于什么是redis, ...