Under construction

Still being built. Things may change

Reversal of the Discharge

Time limit1sMemory limit1024 MB

Summary
Choose actions within a slack budget to maximize Yeongdo's discharge lead over Jonghyeon, or minimize his lag if no lead is possible.
Level

Medium5 of 10

Topics
Dynamic programming, Implementation
Solved
No attempts yet

Problem

Yeongdo enlisted in the Republic of Korea Army to fulfill his national defense duty. After finishing basic training and advanced training and being assigned to a unit, he found that his friend Jonghyeon, who had enlisted earlier, was already there.

Yeongdo could not accept that Jonghyeon would be discharged first. So he made a plan to be discharged before Jonghyeon using the following systems and regulations.

  • Early discharge: Yeongdo can use accumulated leave to move up his discharge date in his final months of service.
  • Discipline training camp: If Jonghyeon is punished and sent to a discipline training camp, his discharge is delayed by the number of days he spends there.
  • Fixed-term noncommissioned officer: By applying to be a fixed-term noncommissioned officer, a soldier can stay on as a sergeant first class for 6 to 48 months and serve the nation's security a little longer. For the fixed-term NCO application, 1 month counts as 30 days.
    • Because the units of Yeongdo and Jonghyeon are short of personnel, multiple fixed-term NCO applications can be submitted. In this case, the service period is not limited by the period restriction and accumulates. For example, if two applications for 48 months as sergeant first class are written and submitted, the service period becomes 96 months.

Yeongdo intends to use these systems and regulations appropriately to bring forward his own discharge date or delay Jonghyeon's. If others learn of the plan, Yeongdo's own discharge could be delayed, so he will continue the plan only within the slack he can use to carry it out in secret.

Even if all the accumulated leave is used, Yeongdo's discharge date does not go past his enlistment date. Even if Yeongdo uses the maximum leave he can obtain for early discharge, he is discharged after the day he makes the plan.

Input

On the first line, three integers Y1Y_1, M1M_1, D1D_1 are given in the format YYYY MM DD, where Jonghyeon's scheduled discharge date is Y1Y_1 year M1M_1 month D1D_1 day.

On the second line, three integers Y2Y_2, M2M_2, D2D_2 are given in the format YYYY MM DD, where Yeongdo's scheduled discharge date is Y2Y_2 year M2M_2 month D2D_2 day.

On the third line, the slack TT that Yeongdo can use to carry out the plan and the number of actions NN are given, separated by a space.

From the fourth line, NN lines describe the actions Yeongdo can take for the plan. Each action can be taken at most once.

  • 11 CC VV: Yeongdo spends CC slack to obtain VV days of leave. The leave he obtains is used later for his own early discharge.
  • 22 CC GG: Yeongdo spends CC slack to set up a large conspiracy. Jonghyeon is caught in it, punished, and spends GG days in a discipline training camp.
  • 33 CC MM: Yeongdo spends CC slack to write Jonghyeon's MM-month fixed-term NCO application on his behalf.

The discharge dates change only through the actions above, and no other factors affect them. All given numbers are integers.

Output

If Yeongdo can be discharged before Jonghyeon, print the maximum number of days by which Yeongdo's discharge precedes Jonghyeon's.

If Yeongdo cannot be discharged before Jonghyeon, print the minimum number of days by which Yeongdo's discharge follows Jonghyeon's.

Constraints

  • 1990≤Y1,Y2≤30001990 \le Y_1, Y_2 \le 3000
  • 0≤T≤100000 \le T \le 10000
  • 0≤N≤1000 \le N \le 100
  • 0≤C≤100000 \le C \le 10000
  • 1≤V≤301 \le V \le 30
  • 1≤G≤151 \le G \le 15
  • 6≤M≤486 \le M \le 48
  • No invalid dates are given.
  • Jonghyeon's scheduled discharge date is always earlier than Yeongdo's scheduled discharge date.
  • All numbers in the input are integers.

Hint

A year is a leap year if it is a multiple of 4 but not a multiple of 100, or if it is a multiple of 400. In a leap year, February 29 exists, so the year has 366 days.

Examples2

  1. Example 1

    Input
    2024 01 03
    2024 07 29
    10 5
    2 8 1
    1 3 3
    3 15 6
    1 7 8
    2 1 3
    
    Expected output
    197
    
  2. Example 2

    Input
    2024 03 09
    2024 03 10
    1 2
    1 1 1
    3 1 6
    
    Expected output
    179