원형 철도 위 두 역 사이를 이동하려는 승객 요청들이 주어질 때, 모든 요청을 처리하기 위해 사야 하는 최소 티켓 묶음 수를 구한다.
어려움8이분 탐색그리디누적 합구현아직 제출이 없습니다시간 제한4초메모리 제한256 MBIn Republic of JOI, there are N stations numbered from 1 to N. They are located clockwise on a circular railway in order.
There are N types of train tickets numbered from 1 to N. By using one ticket of type i (1 ≤ i ≤ N − 1), one person can travel from the station i to the station i + 1, or from the station i + 1 to the station i. By using one ticket of type N, one person can travel from the station 1 to the station N, or from the station N to the station 1. We can only buy a package of N tickets consisting of one ticket for each type.
You are working at a travel agency in Republic of JOI. Your task is to arrange tickets for customers.
Today, you have M requests for arranging tickets. The i-th request says Ci people want to travel from the station Ai to the station Bi. These Ci people need not to take the same route when they travel.
You want to know the minimum number of packages of tickets you need to buy in order to deal with all the requests.
Given the number of stations and information of requests, write a program which calculates the minimum number of packages of tickets you need to buy.
Read the following data from the standard input.
Write one line to the standard output. The output contains the minimum number of packages of tickets you need to buy.