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, KF1F2F3F4, 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个人买东西去柜台是否能全部找钱,k表示要付的钱,后面的数对应钞票1,5,10,20,50的数量,起初柜台里是没钱的。

思路:必须先判断能不能找零,之后才能拿钱,一开始没注意一直卡在第九组数据。如果第一个人给的钱不能刚好支付完,则没有零钱找给他,所以这种情况就直接no,后面假装输入一下就可以continue掉。如果需要找零钱,先找面值最大的,如果最后能找完,则收下他付的钱。
代码如下:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n;
int s[5]={1,5,10,20,50};
int v[5]={0};
int a[5];
long long int k;
cin>>n;
int flag=0,c=0;
while(n--)
{
c++;
cin>>k;
long long int t=0;
for(int i=0;i<5;i++)
{
cin>>a[i];
t+=a[i]*s[i];
}
if(flag)
continue;
k=t-k;
if(k==0)
{
for(int i=0;i<5;i++)
v[i]+=a[i];
}
else
{
if(c==1)
{
flag=1;
continue;
}
while(k>0)
{
while(k>=s[4]&&v[4])
{
k-=s[4];
v[4]--;
}
if(k==0)
break;
while(k>=s[3]&&v[3])
{
k-=s[3];
v[3]--;
}
if(k==0)
break;
while(k>=s[2]&&v[2])
{
k-=s[2];
v[2]--;
}
if(k==0)
break;
while(k>=s[1]&&v[1])
{
k-=s[1];
v[1]--;
}
if(k==0)
break;
while(k>=s[0]&&v[0])
{
k-=s[0];
v[0]--;
}
if(k==0)
break;
if(k>0)
{
flag=1;
break;
}
}
if(flag==0)
{
for(int i=0;i<5;i++)
v[i]+=a[i];
}
}
}
if(flag==1)
cout<<"no"<<endl;
else
cout<<"yes"<<endl;
return 0;
}

  

Gym - 100989H的更多相关文章

  1. Gym - 100989H (贪心)

    After the data structures exam, students lined up in the cafeteria to have a drink and chat about ho ...

  2. 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 ...

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

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

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

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

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

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

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

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

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

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

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

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

  9. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

随机推荐

  1. python str byte 转换

    # bytes object b = b"example" # str object s = "example" # str to bytes bytes(s, ...

  2. CF 977E Cyclic Components

    E. Cyclic Components time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  3. 自己配置 vue 项目 知识体系(自己写脚手架 类似 vue-cli )

    简单的目录结构: |-index.html |-main.js 入口文件 |-App.vue vue文件,官方推荐命名法 |-package.json 工程文件(项目依赖.名称.配置) npm ini ...

  4. 记一次windows服务开发中遇到的问题

    最近在研究windows service和quartz.net,所以迅速在园子大神那里扒了一个demo,运行,安装一切顺利. 但在在App.config配置中增加了数据库连接字符串配置后,服务安装后无 ...

  5. Compile caffe on unbutu 16.0.4

    1. apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhd ...

  6. vue computed的执行问题

    1.在new Vue()的时候,vue\src\core\instance\index.js里面的_init()初始化各个功能 function Vue (options) { if (process ...

  7. HTTP协议工作原理

    HTTP简介        超文本传输协议(HTTP:Hypertext Transport Protocol)是万维网应用层的协议,它通过两个程序实现:一个是客户端程序(各种浏览器),另一个是服务器 ...

  8. C++ 在继承中虚函数、纯虚函数、普通函数,三者的区别

    1.虚函数(impure virtual) C++的虚函数主要作用是“运行时多态”,父类中提供虚函数的实现,为子类提供默认的函数实现. 子类可以重写父类的虚函数实现子类的特殊化. 如下就是一个父类中的 ...

  9. webstorm项目的structure

    https://www.jetbrains.com/help/webstorm/2016.1/symbols.html

  10. 自动微分(AD)学习笔记

    1.自动微分(AD) 作者:李济深链接:https://www.zhihu.com/question/48356514/answer/125175491来源:知乎著作权归作者所有.商业转载请联系作者获 ...