After the data structures exam, students lined up in the cafeteria to have a drink and chat about how much they have enjoyed the exam and how good their professors are. Since it was late in the evening, the cashier has already closed the cash register and does not have any change with him.

The students are going to pay using Jordanian money notes, which are of the following types: 1, 5, 10, 20, 50.

Given how much each student has to pay, the set of notes he’s going to pay with, and the order in which the students arrive at the cashier, your task is to find out if the cashier will have enough change to return to each of the student when they arrive at the cashier.

Input

The first line of input contains a single integer N (1 ≤ N ≤ 105), the number of students in the queue.

Each of the following N lines describes a student and contains 6 integers, K, F1, F2, F3, F4, and F5, where K represents the amount of money the student has to pay, and Fi (0 ≤ Fi ≤ 100) represents the amount of the ith type of money this student is going to give to the cashier.

The students are given in order; the first student is in front of the cashier.

It is guaranteed that no student will pay any extra notes. In other words, after removing any note from the set the student is going to give to the cashier, the amount of money will be less than what is required to buy the drink.

Output

Print yes if the cashier will have enough change to return to each of the students when they arrive in the given order, otherwise print no.

Examples

Input
3
4 0 1 0 0 0
9 4 1 0 0 0
8 0 0 1 0 0
Output
no
Input
3
9 4 1 0 0 0
4 0 1 0 0 0
8 0 0 1 0 0
Output
yes
题意:第一行输入一个n表示,那个人正在排队,当前售货员没有零钱,接下来又n行,每行有6个数,分别表示所需付的钱,以及1,5,10,20,50元的个数,判断售货员是否可以对每个顾客及时找零。
题解:采用贪心的算法,金额越小越适合找零,能用金额大的找零则用金额大的找零,尽量少用金额小的找零。
具体情况详见代码:
 #include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int main(){
int n;
while(cin>>n)
{
int flag=;
int num1=,num5=,num10=,num20=,num50=,a,b,c,d,e,k;
for(int i=;i<n;i++)
{
cin>>k>>a>>b>>c>>d>>e;
int sum=a+b*+c*+d*+e*; //顾客所拥有的总金额
if(k==sum) //若刚好够,则全部给售货员
{
num1+=a;
num5+=b;
num10+=c;
num20+=d;
num50+=e;
}
if(k<sum)
{
sum=sum-k; //需要找零的钱
if(sum>=&&num50>)
{
if(sum>=num50*)
{ sum-=num50*; //用50找零后还需要再找的零钱
num50=;
}
else
{
sum=sum%;
num50-=sum/;
}
}
if(sum>=&&num20>)
{
if(sum>=num20*)
{ sum-=num20*;
num20=;
}
else
{
sum=sum%;
num20-=sum/;
}
}
if(sum>=&&num10>)
{
if(sum>=num10*)
{ sum-=num10*;
num10=;
}
else
{
sum=sum%;
num10-=sum/;
}
}
if(sum>=&&num5>)
{
if(sum>=num5*)
{ sum-=num5*;
num5=;
}
else
{
sum=sum%;
num5-=sum/;
}
}
if(sum>num1)
flag=;
else
num1-=sum;
num1+=a;
num5+=b;
num10+=c;
num20+=d;
num50+=e;
}
}
if(flag==) cout<<"no"<<endl;
else cout<<"yes"<<endl;
}
return ;
}

Gym - 100989L的更多相关文章

  1. Gym 100989L (DFS)

    AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tr ...

  2. dfs Gym - 100989L

    AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tr ...

  3. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  4. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  5. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  6. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  7. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  8. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

  9. Gym 100917J---dir -C(RMQ--ST)

    题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...

随机推荐

  1. react中如何使用动画效果

    在react中想要加入动画效果 需要引入 import {CSSTransitionGroup} from 'react-transition-group' //加入react 动画包 import ...

  2. python与C,在写程序时踩过的坑!

    1.  python与C有很多相似之处, 其一就是指针的大量应用,  因此在使用临时变量保存数据, 并将临时变量传递给其他变量时需要创建内存; 例如,在C中, char *temp 每次获取到不同的字 ...

  3. Java Integer 与 int 深刻理解

    今天在做Object 自动转为Integer 类型之后的判断,遇到一个不理解的点,当数值超过127之后,两个数值相同的Object 对象用 == 判断的结果是false. Object a = 128 ...

  4. [转帖]SPU、SKU、ID,它们都是什么意思,三者又有什么区别和联系呢?

    SPU.SKU.ID,它们都是什么意思,三者又有什么区别和联系呢? http://blog.sina.com.cn/s/blog_5ff11b130102wx0p.html 电商时代,数据为王. 所以 ...

  5. 剑指offer(7)

    今天的几道题目都是关于斐波那契数列的. 题目1: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 传统的方法采用递归函数,这种 ...

  6. java学习之—排序

    package test3; public class Sort{ /** * 冒泡排序 * @param array */ public void bubbleSort(int[] array) { ...

  7. NFS配置与搭建

    参考: Linux下NFS服务器的搭建与配置 https://www.cnblogs.com/liuyisai/p/5992511.html http://blog.51cto.com/hongten ...

  8. git ignore 忽略 idea文件

    下载了项目组的代码之后发现,一个问题,一编译就生成了很多的 .idea文件夹 还有 target文件夹,这些是不需要提交到git上的, 需要提交的时候屏蔽一下,所以需要建立一个ignore文件列表把他 ...

  9. Spring Boot基础:Spring Boot简介与快速搭建(1)

    1. Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的创建.运行.调试.部署等. Spring Boot默认使用tomca ...

  10. commons-lang

    今天在编码的过程中,对于null,采用==null进行判断.并且为了过滤"",使用了str.trim().length()==0,当str为null时,报空指针异常. 于是决定使用 ...