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. c++的继承方式

    c++的继承,因为学完过的时间太长,忘了,现在再温习一下. c++的继承方式 1. 公有继承(public) 2. 私有继承(private) 3. 保护继承(protected) 从一个基类派生的继 ...

  2. “四则运算生成程序——GUI支持和部分功能改进”链接

    项目成员:张金生     张政 <正文随笔发布在张金生博客中> 四则运算生成程序——GUI支持和部分功能改进

  3. PAT 1054 求平均值

    https://pintia.cn/problem-sets/994805260223102976/problems/994805272659214336 本题的基本要求非常简单:给定N个实数,计算它 ...

  4. jmeter 多线程组间变量共享

    jmeter的线程组之间是相互独立的,各个线程组互不影响,所以线程组A中输出的参数,是无法直接在线程组B中被调用的. 但是有时为了方便管理,我们可能是把各个接口单独存放在不同的线程组中.拿Cookie ...

  5. 微信小程序组件 分页菜单点击请求

    //JS data: { navNum:0, navList: [ { id: 1, name: '已预约' }, { id: 2, name: '已消费' }, { id: 3, name: '已取 ...

  6. Java-JDBC.mysql 工具类 读取本地文件配置

    引用 mysql-connector-jav 的jar 配置文件为  database.propertties .  格式如下 driverClass=com.mysql.jdbc.Driver ur ...

  7. latex添加eps文档

    latex添加图像时,要将.eps文档放在当前文件夹中,然后使用: % For one-column wide figures use\begin{figure}\begin{center}% Use ...

  8. Ubuntu实用软件安装[转]

    Gedit编辑器配置 Ubuntu14.04从安装软件到卸载软件,删除安装包 linux wget 命令用法详解(附实例说明) ==================================== ...

  9. JDK中的SimpleDateFormat线程非安全

    在JDK中使用SimpleDateFormat的时候都会遇到线程安全的问题,在JDK文档中也说明了该类是线程非安全的,建议对于每个线程都创建一个SimpleDateFormat对象.如下面一个Case ...

  10. VIM 模板

    Vim实现自动加载模版功能可以有很多的方法,比如利用插件和AutoCmd等.根据文件名自动加载模板的功能利用网上某大牛自己写的插件实现,我针对Java代码进行简单地修改,以实现模板中的Java主类类名 ...