Solidarity of the Happy Cats

시간 제한1초메모리 제한2048 MB

요약
전선을 원래 순서대로 최소 개수의 칸에 배치하되, 영향을 주는 신호 종류를 가진 전선의 범위 안에 다른 전선이 들어가지 않도록 해야 한다.
난이도

어려움10점 중 8점

유형
동적 계획법, 그리디, 이분 탐색
정답자
아직 제출이 없습니다

문제

Professor Pattengale has run into a huge problem - his robot is getting wrong digital signals, causing it to move around unpredictably! His circuitry students, the Happy Cats, conclude that the wires in the motherboard are too close together. As an electrical current passes through a wire, it creates a magnetic field that induces currents in other nearby wires, causing false signals.

The Engineering Family finds that the motherboard has NN slots lined up in a row, and each slot has exactly one wire installed in it. Each wire has range r_ir\_i, covering r_ir\_i slots both to the left and to the right of that wire; other wires contained in this range may be affected by its magnetic field. Each wire also has a type of signal s_is\_i running through it.

There are also MM different types of signals being passed throughout the wires. Certain types of signals can impact other types of signals. For two different types aa and bb, if a signal of type bb can impact a signal of type aa, then a wire with signal type aa cannot be within the range of any wire of signal type bb, otherwise there will be interference. If type bb does not impact a signal of type aa, then wires with signal type aa are permitted to be within the range of any wire with signal type bb. Interference only happens when a signal of type bb impacts a signal with type aa, and the wire with signal type aa is within the range of the wire with signal type bb.

It is possible for a signal of type aa to impact a signal of type bb, but for a signal of type bb to not impact a signal of type aa. It is also possible for a signal of type aa to impact other wires of type aa.

The Engineering Family wants to create a new motherboard for the wires. The new motherboard will also comprise a single row of slots. All NN wires must be installed in the same order from left to right in the new motherboard as they were in the old motherboard. It is only possible to install at most one wire in any given slot. Some slots may be left empty. There must be no interference.

Given the original layout of the NN wires, compute the minimum number of slots in the new motherboard such that there is no interference.

입력

The first line of input contains two positive integers, NN and MM (1≤M≤N≤2001 \le M \le N \le 200).

NN lines follow, each with a pair of integers r_ir\_i and s_is\_i (1≤r_i≤109,1≤s_i≤M1 \le r\_i \le 10^9, 1 \le s\_i \le M), the range and signal type of the ii-th wire. The wires are presented in order from left to right as they are installed in the old motherboard.

MM lines follow, each containing a binary string of length MM. The iith line describes which signals impact signals of type ii. If the jjth digit is a 11, then signals of type jj do not impact signals of type ii. Otherwise, signals of type jj do impact signals of type ii.

출력

Output a single integer: the minimum number of slots the new motherboard needs to have to avoid interference.

예제3

  1. 예제 1

    입력
    3 3
    6 1
    3 2
    4 3
    010
    101
    100
    
    예상 출력
    6
    
  2. 예제 2

    입력
    3 3
    6 1
    3 2
    4 3
    010
    100
    100
    
    예상 출력
    7
    
  3. 예제 3

    입력
    4 2
    4 2
    2 1
    1 1
    4 2
    11
    10
    
    예상 출력
    6