Time Shift
Time limit3sMemory limit256 MB
Given daily clock shifts in n cities over a year, compute the sum over all hours of the pairwise absolute differences of each city's elapsed time from the capital.
- Level
Hard8 of 10
- Topics
- Sorting, Prefix sum, Math, Simulation
- Solved
- No attempts yet
Problem
Flatland has cities. Flatland is not on Earth, so a day there lasts hours and a year has days, that is exactly hours.
Each city in Flatland has its own government, and every city has its own laws about shifting clocks. In some cities, for example, there are not only summer and winter time but also spring, autumn, mid-season, holiday, and weekend time. Since the laws differ from city to city, the time in different cities often differs greatly. Naturally, having many different times inconveniences the country's residents.
The city numbered is the capital of Flatland. To make communication with other countries convenient, the time there is never shifted. It is also known that the difference between the time of any other city and the time of the capital never exceeds hours in absolute value.
Let us describe how time is measured and how clocks are shifted. In the center of each city stands a huge clock. This clock shows the number of the current day in that city and the current hour. Minutes and seconds are not shown, since the government considers them unimportant. At the start of the year, by an old tradition, the time in all cities is synchronized, and the year in every city begins at midnight, that is, all clocks show zero and the first day begins. Each time midnight arrives in a city, a new day begins there, that is, the number of the current day in that city changes. If a shift day arrives in a city, then at the first noon of that day (when the clock shows hours) the clock is shifted by the amount specified in the law. A shift never changes the number of the current day, but if the shift is hours forward, then midnight arrives immediately and, accordingly, the next day begins in that city.
The Special Commission for Assessing the Inconvenience of Flatland has developed a numerical measure of the inconvenience that arises. The hourly inconvenience in the country is the sum of the absolute differences of times over all pairs of cities during one hour. Namely, let , where is the number of the current day in city and is the current hour in city . Sum the value over all unordered pairs of distinct cities . The resulting sum is the hourly inconvenience in the country. The yearly inconvenience is the sum of the hourly inconveniences over all hours of the year, that is, over the hours from the start of the year.
The schedules of time shifts in the cities during the year are known. Your task is to compute the yearly inconvenience. At the start of the year the times in all cities coincide and the year begins at midnight, that is, the clocks show zero. A time shift in any city happens only at local noon, that is, at hours. Shifts connected with synchronizing clocks at the start of the year are not listed, since they are a major cultural event and happen all at once with the arrival of the new year.
Input
The first line contains four integers , , , and (; ; ; ). The next lines describe time shifts. Each description consists of three numbers , , and : the number of the day on which the shift happens, the number of the city in which the shift happens, and the number of hours by which the time is shifted (; ; ; ). Each shift is by at most 200 hours.
The input data is guaranteed to be valid. Each city has at most one shift per day. Flatlanders number the days of the year starting from one.
Output
Print a single integer: the yearly inconvenience. It is guaranteed that the answer does not exceed .