PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BF0000038. Tuyển nhân viên thu ngân (Cashier Employment)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tuyển nhân viên thu ngân (Cashier Employment)

Cashier Employment

Source: POJ

Version: Phuoc Hung OJ Extended

Problem Statement

A store is open for 24 hours. Hour hh needs at least RhR_h cashiers. Each of NN applicants has a fixed start hour tt and works 8 consecutive hours, possibly across midnight. Hire the minimum number meeting all hourly requirements, or print No Solution if impossible.

Shift coverage wraps around the 24-hour boundary.

This Extended version removes the leading count of independent test cases.

Input

Read 24 integers R0,…,R23R_0,\ldots,R_{23}, then NN and NN start-hour integers tit_i.

Output

Print the minimum number to hire, or No Solution if impossible.

Subtasks

  • Subtask 1 (20 points): $0\le N\le12; 0\le R_h\le1000 (0\le h<24); 0\le t_i\le23$.
  • Subtask 2 (30 points): $0\le N\le150; 0\le R_h\le1000 (0\le h<24); 0\le t_i\le23$.
  • Subtask 3 (50 points): $0\le N\le1000; 0\le R_h\le1000 (0\le h<24); 0\le t_i\le23$.

Examples

Input

1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
5
0 23 22 1 10

Output

1

Explanation

The sample requirements at hours 00, 22, 66, and 2323 can be covered by one suitably timed 8-hour shift when the shift wraps around midnight. The reference code evaluates all hourly requirements for the minimum.