Codeforces 839B Game of the Rows - 贪心

Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has nrows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}.

A row in the airplane

Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats.

Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied.

Input

The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively.

The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group.

It is guaranteed that a1 + a2 + ... + ak ≤ 8·n.

Output

If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes).

You can choose the case (lower or upper) for each letter arbitrary.

Examples
input
2 2
5 8
output
YES
input
1 2
7 1
output
NO
input
1 2
4 4
output
YES
input
1 4
2 2 1 2
output
YES
Note

In the first sample, Daenerys can place the soldiers like in the figure below:

In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group.

In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats.

In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6).


  题目大意 (题目太长简(长)洁(了),无法概述)

  因为4个挨在一起的位置很复杂,所以考虑先把所有4个位置挨在一起的座位干掉。

  当存在某个队还没被安排的人数大于等于4,那么就扔一个这样的座位给他们,如果不存在这样的座位,那就分配一个两个座的座位。如果分配出了问题,直接输出NO吧。

  然后考虑剩余人数为3的队伍,要么安排4座,要么安排1个两座剩1人(余下来的一人等会儿考虑,并加入暂时无法安排的人)。

  此时,所有队伍剩余人数不超过2人。

  继续考虑剩余人数为2的队伍,如果存在4座,那么旁边就会多出可以坐1人的座位,否则就安排坐2座,如果没有2座,就把暂时无法安排的人数加2。

  最后所有剩下的座位显然都是要1个人1个人地坐了,所以计算一下,判断还可以坐的人数是否大于等于暂时无法安排的人数。

Code

 1 /**
 2  * Codeforces
 3  * Problem#839B
 4  * Accepted
 5  * Time: 15ms
 6  * Memory: 2100k 
 7  */
 8 #include <bits/stdc++.h>
 9 using namespace std;
10 
11 int n, k;
12 int rest[5], cnt[5]; 
13 
14 inline void init() {
15     scanf("%d%d", &n, &k);
16     rest[2] = n << 1, rest[4] = n;
17     for(int i = 1, x; i <= k; i++) {
18         scanf("%d", &x);
19         while(x > 2) {
20             if(rest[4])
21                 rest[4]--, x -= 4;
22             else if(rest[2])
23                 rest[2]--, x -= 2;
24             else {
25                 puts("NO");
26                 exit(0);
27             }
28         }
29         if(x)
30             cnt[x]++;
31     }
32 }
33 
34 inline void solve() {
35     while(cnt[2]--) {
36         if(rest[4])
37             rest[4]--, rest[1]++;
38         else if(rest[2])
39             rest[2]--;
40         else
41             cnt[1] += 2;
42     }
43     puts((rest[4] * 2 + rest[2] + rest[1] >= cnt[1]) ? ("YES") : ("NO"));
44 }
45 
46 int main() {
47     init();
48     solve();
49     return 0;
50 } 
posted @ 2017-08-15 14:43  阿波罗2003  阅读(422)  评论(0编辑  收藏  举报