#P15223. [SWERC 2017] Shattered Cake

[SWERC 2017] Shattered Cake

题目描述

A rectangular cake is transported via a truck to a restaurant. On the way to the destination, the truck hits a pothole, which shatters the cake in N N perfectly rectangular pieces of width wi w_i and length li l_i , for 1iN 1 \leq i \leq N .

At the destination, the damage is assessed, and the customer decides to order a replacement cake of the same dimensions. Unfortunately, the original order form was incompletely filled and only the width W W of the cake is known. The restaurant asks for your help to find out the length L L of the cake. Fortunately, all pieces of the shattered cake have been kept.

输入格式

The input consists of the following integers:

  • on the first line, the width W W of the cake;
  • on the second line, the number N N of shattered pieces;
  • on each of the next N N lines, the width wi w_i and length li l_i of each piece.

输出格式

The output should be the integer L L .

4
7
2 3
1 4
1 2
1 2
2 2
2 2
2 1
6

提示

Limits

  • 1N5000000 1 \leq N \leq 5\,000\,000 ;
  • 1W,L10000 1 \leq W, L \leq 10\,000 ;
  • for each 1iN 1 \leq i \leq N , 1wi,li10000 1 \leq w_i, l_i \leq 10\,000 .