The Dragon Warrior
Difficulty: Hard


Po has one last task to achieve ultimate glory and be termed The Dragon Warrior of Kung Fu. Po has Tigress, Crane, Mantis, Viper and Monkey to help him with this. Po needs to gain atleast Q calories of energy by the end of this task to defeat Tai Lung. Help us find out if he does !

There are K items hidden in the cells of a grid containing (A+1) rows and (Z+1) columns. Rows are number 0 through A and columns 0 through Z. These K items appear one at a time after the previous item is found. Po can choose which cell he wants to start on. In every round Po can move to any cell in the same row or the same column or stay in the previous cell. If Po is unable to get to an item in a round, it is assured that one of the remaining 5 will. Thus, there are K rounds in all.

Po gains calories each time an item is found! Po's gain is A + Z, minus the Manhattan distance of his cell from the cell where the item was found. So if the item was found at cell (j,k) and Po is situated at (s,r), then his gain would be A + Z - | S - J | - | R - K |, where | | denotes absolute value.

You are given the integers A, Z, Q and K. You are also given K pairs integers, (X, Y) where X(row) and Y(column) denote the location where the next item is hidden. Note that the items will appear in the same sequece.

Calculate the maximum calories gained by Po when all the items have been found.

If he gains atleast Q calories of energy print 1. Otherwise print 0.

###Input :

The input consists of K+1 lines.

The first line has A Z Q and K separated by spaces.

The next K lines consist of two space separated integers denoting the X and Y coorinates of item respectively.

###Output :

Your output should be **1** if Po gains atleast Q calories. If not, **0**.

###Constraints :

- A and Z lie between 1 and 1,000,000.
- Q lies between 1 and 1,000,000,000.
- K lies between 0 and 50.

###Sample Input :

2 2 6 2

0 0

2 1

###Sample Output :

1

###Explanation :

The grid consists of 3 rows and 3 columns. Since Po gets to choose where he wants to start he can start with 0,0 to gain a maximum strength of 4 for the first item. Then the second item is on 2,1.

So Po moves to cell 2,0. He gains 2 + 2 - | 2 - 2 | - | 1 – 0 | = 3 calories. Thus his total gain would be 4 + 3 = 7.

Since 7 > 6 the output is 1.

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include <cstring>
    #include <vector>

    using namespace std;

    int k, a, z, q;
    int dp[51][51][51];
    vector <int> x;
    vector <int> y;

    int rec(int p, int r, int s)
    {
        int &res = dp[p][r][s];

        if (res == -1)
        {
            if (s == k)
            {
                res = 0;
            }
            else
            {
                int dif = a + z - abs( x[p] - x[s]) - abs( y[r] - y[s]);
                res = 0;

                for (int na = 0; na < k; na++) {
                    res = max(res, dif + rec(na,r,s + 1) );
                }

                for (int nb = 0; nb < k; nb++) {
                    res = max(res, dif + rec(p,nb,s + 1) );
                }
            }
        }
        return res;
    }
    int main()
    {
        int row, col, q;

        scanf("%d %d %d %d", &a, &z, &q, &k);

        memset(dp, -1, sizeof(dp));

        int ans=0;

        for(int i=0; i<k; i++)
        {
            scanf("%d %d", &row, &col);
            x.push_back(row);  //add X coordinates to vector x
            y.push_back(col);  //add Y coordinates to vector y
        }

        for(int i=0; i<k; i++)
        {
            for(int j=0; j<k; j++)
            {
                ans=max(ans, rec(i, j, 0));
            }
        }

        if(ans>=q)
            printf("1");
        else
            printf("0");

        return 0;
    }