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. C51学习笔记

    转自:http://blog.csdn.net/gongyuan073/article/details/7856878 单片机C51学习笔记 一,   C51内存结构深度剖析 二,   reg51.头 ...

  2. Teamcity部署.net服务“无法连接到远程服务器”解决方式

    在公司Teamcity上执行自动部署.net服务的时候,发现Teamcity在启动default.aspx的时候报错了,提示:使用“0”个参数调用“GetResponse”时发生异常:“无法连接到远程 ...

  3. [转帖] IIS 与 HTTP/2 的介绍.

    HTTP/2 on IIS https://blogs.iis.net/davidso/http2 Friday, September 11, 2015 Windows 10 HTTP2 In Oct ...

  4. 2013长沙网赛E题Travel by Bike

    题目链接:http://acm.zju.edu.cn/changsha/showProblem.do?problemId=26 题意:一个人从一个地方到另一个地方,长度为L,每小时速度为speed,周 ...

  5. 如何从 GitHub 上下载单个文件夹?

    比如别人把一些资料传到 GitHub 上做分类归档,我怎么才能下载单一分类文件夹? 点击 Raw ,如果是不能预览的文件就会自动下载,如果是文件或者代码什么的,会在浏览器中显示,但可以复制浏览器中链接 ...

  6. param 是获取请求传递过来的参数

  7. IBatisNet 缓存使用

    参考:http://www.cnblogs.com/xiaogangqq123/archive/2011/06/30/2094905.html <?xml version="1.0&q ...

  8. jingchi.ai 2017.11.25-26 Onsite面试

    时间:2017.11.25 - 11.26 地点:安徽安庆 来回路费报销,住宿报销. day1: 大哥哥问了我一个实际中他们遇到的问题.有n个点,将点进行分块输出,输出各个块的均值点.具体就是100* ...

  9. hdu2138 Miller_Rabin

    Description   Give you a lot of positive integers, just to find out how many prime numbers there are ...

  10. BZOJ 3190 赛车 | 计算几何

    BZOJ 3190 赛车 题面 这里有一辆赛车比赛正在进行,赛场上一共有N辆车,分别称为个g1,g2--gn.赛道是一条无限长的直线.最初,gi位于距离起跑线前进ki的位置.比赛开始后,车辆gi将会以 ...