This page is still under construction.

Parts of this page are still being built. What you see may change.

Holidays

Time limit2sMemory limit64 MB

Summary
Choose a weekly day off to maximize non-working days over a contract of E days, counting holidays across leap and normal years.
Level

Medium7 of 10

Topics
Math, Prefix sum, Implementation, Number theory
Solved
No attempts yet

Problem

On a planet in the Alpha Centauri system, a week has AA days and a year has BB days. Years are numbered with consecutive positive integers: 1,2,3,…1, 2, 3, \ldots. Years numbered C1,C2,…,CNC_1, C_2, \ldots, C_N are leap years and have (B+1)(B+1) days. In a year, the days numbered D1,D2,…,DMD_1, D_2, \ldots, D_M are holidays. If a holiday falls on day (B+1)(B+1) of the year, it is observed only in leap years. The first day of the first year is the first day of the week.

A resident of the planet has decided to take a new job. Under the employment contract, he will hold this job for EE days, starting from the first day of year 1. By the contract, he may choose one day of the week (from 1 to AA) as his day off. Holidays are also counted as non-working days. The resident wants to choose his day off so that during the contract period he has the maximum number of non-working days.

Write a program that finds this day of the week and computes the corresponding number of non-working days.

Input

The first line contains AA and BB, the number of days in a week and in a non-leap year, separated by a space (1≤A≤25001\le A \le 2500, 1≤B≤100001\le B \le 10000). The second line contains NN, the number of leap years, and the third line contains the numbers C1,C2,…,CNC_1, C_2, \ldots, C_N of the leap years in increasing order (0≤N≤50000\le N \le 5000, 1≤C1<C2<…<CN≤1071 \le C_1 < C_2 < \ldots < C_N \le 10^7). The next line contains MM, the number of holidays in a year, and the line after it contains D1,D2,…,DMD_1, D_2, \ldots, D_M in increasing order (1≤D1<D2<…<DM≤B+11 \le D_1 < D_2 < \ldots < D_M \le B+1). The last line contains EE (1≤E≤1091\le E \le 10^9). The contract lasts no more than 10710^7 years.

Output

Output two numbers separated by a space: the number of the day of the week that is most advantageous to take as a day off, and the corresponding number of non-working days during the contract period. If there are several answers, output any of them.

Examples2

  1. Example 1

    Input
    7 13
    1
    2
    2
    1 14
    29
    
    Expected output
    1 8
    
  2. Example 2

    Input
    3 9
    0
    
    3
    1 4 7
    19
    
    Expected output
    2 13