206. Tourist

 

John is going to a tourist meeting of pupils in his school. In his class he was made responsible for the tents. At home he found 3 tents: the first one weighs a1 kilograms and accommodates b1 people, the second tent weighs a2 kilograms and accommodates b2 people, the third tent weighs a3 kilograms and accommodates b3 people.

There are k pupils in John 's class. Find out if John can choose tents such that all people can fit in them. Take into account that the selected tents should weigh no more than w kilograms in total.

 

Input. The first line contains two integers: k and w (1 ≤ k ≤ 15, 1 ≤ w ≤ 30). The second line contains six integers: a1, b1, a2, b2, a3, b3 (1 ≤ a1, a2, a3 ≤ 10, 1 ≤ b1, b2, b3 ≤ 15).

 

Output. Print YES if it is possible to choose tents as desired, and NO otherwise.

 

Sample input

Sample output

10 10

5 5 6 6 4 5

YES

 

 

SOLUTION

elementary problem – conditional statement

 

Algorithm analysis

There are only three tents, so we can consider all possible alternatives of arranging pupils in tents. Lets consider all possible arrangements here:

·        only in the first tent;

·        only in the second tent;

·        only in the third tent;

·        only in the first and in the second tent;

·        only in the first and in the third tent;

·        only in the second and in the third tent;

·        in all three tents.

All described alternatives can be considered with seven conditional statements. Declare the variable flag, initially set it to 0 (all pupils can not be placed in tents). Then check the possibility of each of seven placements.

For example the condition of arranging all the pupils only in the second tent will look this way:

if ((b2 >= k) && (a2 <= w)) flag = 1;

The condition of arranging all the pupils only in the first and in the third tent will look this way:

if ((b1 + b3 >= k) && (a1 + a3 <= w)) flag = 1;

Depending on the variable flag after checking all seven conditions print the answer.

 

Algorithm realization

Do a complete search of all possible ways to arrange the pupils in the tents.

 

scanf("%d %d",&k,&w);

scanf("%d %d %d %d %d %d",&a1,&b1,&a2,&b2,&a3,&b3);

if ((b1 >= k) && (a1 <= w)) flag = 1;

if ((b2 >= k) && (a2 <= w)) flag = 1;

if ((b3 >= k) && (a3 <= w)) flag = 1;

if ((b1 + b2 >= k) && (a1 + a2 <= w)) flag = 1;

if ((b1 + b3 >= k) && (a1 + a3 <= w)) flag = 1;

if ((b2 + b3 >= k) && (a2 + a3 <= w)) flag = 1;

if ((b1 + b2 + b3 >= k) && (a1 + a2 + a3 <= w)) flag = 1;

if (flag) printf("YES\n"); else printf("NO\n");