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. mac下使用marsedit写博客

    在Windows习惯了使用Windows live write写好博客,然后发布上去,到了mac下,发现了一个很好的替代品  MarseEdit 下载安装后,打开出现如下解码,选择 I already ...

  2. tomcat介绍

    Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由java语言编写,需要运行在jvm虚拟机中.之所以Java的应用 ...

  3. 6/7 sprint2 看板和燃尽图的更新

  4. 软工网络15团队作业4——敏捷冲刺日志的集合贴(Alpha阶段)

    Alpha阶段 第 1 篇 Scrum 冲刺博客 第 2 篇 Scrum 冲刺博客 第 3 篇 Scrum 冲刺博客 第 4 篇 Scrum 冲刺博客 第 5 篇 Scrum 冲刺博客 第 6 篇 S ...

  5. 累计进度条 PSP 饼图

    累计进度条   PSP   饼图 每周例行报告 本周PSP 类别 任务 开始时间 结束时间 被打断时间 总计工作时间 2016年9月24日 读书 构建之法-6.7章 19:00 20:00 2 58m ...

  6. 通过loadrunner将http返回response写入文本txt中

    脚本如下 Action() { int myFile;//lr不支持FILE类型,所以定义一个int类型的file web_reg_save_param("goods_price" ...

  7. TiDB注意事项

    公司最近在上测试的TiDB集群,这款数据库类似MySQL,但又不完全一致,在使用的时候有一下注意事项,在这里记录一下.

  8. java 数据结构与算法---栈

    原理来自百度百科 一.栈的定义 栈是一种只能在一端进行插入和删除操作的特殊线性表:它按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数 ...

  9. SpringBoot(六)_AOP统一处理请求

    什么是AOP AOP 是一种编程范式,与编程语言无关: 将通用逻辑从业务逻辑中分离出来(假如你的业务是一条线,我们不在业务线上写一行代码就能完成附加任务!我们会把代码写在其他的地方): 具体实现 (1 ...

  10. Struts1 工作流程

    一个老项目的维护 , 需要学习一下 Struts1. struts1运行步骤 1.项目初始化:项目启动时加载 web.xml,struts1 的总控制器 ActionServlet 是一个 Servl ...