Under construction

Still being built. Things may change

Cannonball

Time limit2sMemory limit1024 MB

Summary
Given a number line of targets and jump pads, count how many targets Bessie breaks while bouncing from her starting cell.
Level

Medium6 of 10

Topics
Simulation, Array
Solved
No attempts yet

Problem

Bessie has mastered the art of turning into a cannonball and bouncing along a number line of length NN (1≤N≤1051 \leq N \leq 10^5) with locations numbered 1,2,…,N1, 2, \dots, N from left to right. She starts at integer location SS (1≤S≤N1 \leq S \leq N) bouncing to the right with a starting power of 1. If Bessie has power kk, her next bounce lands at a distance kk forward from her current location.

Every integer location from 1 to NN is either a target or a jump pad. Each target and jump pad has an integer value from 0 to NN inclusive. A jump pad with value vv increases Bessie's power by vv and reverses her direction. A target with value vv breaks if Bessie lands on it with a power of at least vv. Landing on a target does not change her power or direction. A broken target stays broken, and Bessie can still bounce on it without changing power or direction.

If Bessie bounces forever or until she leaves the number line, how many targets will she break?

If Bessie starts on a target she can break, she breaks it immediately. Likewise, if she starts on a jump pad, the pad's effect applies before her first jump.

Input

The first line contains NN and SS, where NN is the length of the number line and SS is Bessie's starting location.

The next NN lines describe each location. The ii-th line contains integers qiq_i and viv_i, where qi=0q_i = 0 if location ii is a jump pad and qi=1q_i = 1 if location ii is a target, and viv_i is the value of location ii.

Output

Output one number, the number of targets that will be broken.

Examples2

  1. Example 1

    Input
    5 2
    0 1
    1 1
    1 2
    0 1
    1 1
    
    Expected output
    1
    
  2. Example 2

    Input
    6 4
    0 3
    1 1
    1 2
    1 1
    0 1
    1 1
    
    Expected output
    3