#P5751. [NOI1999] 01 串

[NOI1999] 01 串

Description

Given 77 integers N,A0,B0,L0,A1,B1,L1N, A_0, B_0, L_0, A_1, B_1, L_1, you are asked to design a binary string S=s1s2sisNS = s_1 s_2 \ldots s_i \ldots s_N that satisfies:

  1. si=0s_i = 0 or si=1s_i = 1, 1iN1 \leq i \leq N.
  2. For any consecutive substring of SS with length L0L_0, namely sjsj+1sj+L01s_j s_{j+1} \ldots s_{j+L_0-1} (1jNL0+11 \leq j \leq N - L_0 + 1), the number of 00s is at least A0A_0 and at most B0B_0.
  3. For any consecutive substring of SS with length L1L_1, namely sjsj+1sj+L11s_j s_{j+1} \ldots s_{j+L_1-1} (1jNL1+11 \leq j \leq N - L_1 + 1), the number of 11s is at least A1A_1 and at most B1B_1.

For example, if $N = 6, A_0 = 1, B_0 = 2, L_0 = 3, A_1 = 1, B_1 = 1, L_1 = 2$, then there exists a binary string S=010101S = 010101 that satisfies all the above conditions.

Input Format

Only one line containing 77 integers, in order: N,A0,B0,L0,A1,B1,L1N, A_0, B_0, L_0, A_1, B_1, L_1 (3N10003 \leq N \leq 1000, 1A0B0L0N1 \leq A_0 \leq B_0 \leq L_0 \leq N, 1A1B1L1N1 \leq A_1 \leq B_1 \leq L_1 \leq N). Adjacent integers are separated by one space.

Output Format

Only one line. If there is no binary string satisfying all conditions, output the integer -1. Otherwise, output the maximum possible number of 11s among all binary strings that satisfy all conditions.

6 1 2 3 1 1 2

3

Hint

Translated by ChatGPT 5