Codeforces Round 215 (Div. 1)
C
Sereja and the Arrangement of Numbers
Codeforces
2000
原题
← BD →
翻译中…

Let's call an array consisting of $*n*$ integer numbers $*a*_{1}$, $*a*_{2}$, $...$, $*a*_{*n*}$, beautiful if it has the following property:

翻译中…
  • consider all pairs of numbers $*x*, *y*$ $(*x* ≠ *y*)$, such that number $*x*$ occurs in the array $*a*$ and number $*y*$ occurs in the array $*a*$;
  • for each pair $*x*, *y*$ must exist some position $*j*$ $(1 ≤ *j* < *n*)$, such that at least one of the two conditions are met, either $*a*_{*j*} = *x*, *a*_{*j* + 1} = *y*$, or $*a*_{*j*} = *y*, *a*_{*j* + 1} = *x*$.
翻译中…

Sereja wants to build a beautiful array $*a*$, consisting of $*n*$ integers. But not everything is so easy, Sereja's friend Dima has $*m*$ coupons, each contains two integers $*q*_{*i*}, *w*_{*i*}$. Coupon $*i*$ costs $*w*_{*i*}$ and allows you to use as many numbers $*q*_{*i*}$ as you want when constructing the array $*a*$. Values $*q*_{*i*}$ are distinct. Sereja has no coupons, so Dima and Sereja have made the following deal. Dima builds some beautiful array $*a*$ of $*n*$ elements. After that he takes $*w*_{*i*}$ rubles from Sereja for each $*q*_{*i*}$, which occurs in the array $*a*$. Sereja believed his friend and agreed to the contract, and now he is wondering, what is the maximum amount of money he can pay.

翻译中…

Help Sereja, find the maximum amount of money he can pay to Dima.

翻译中…

Input

翻译中…

The first line contains two integers $*n*$ and $*m*$ $(1 ≤ *n* ≤ 2·10^{6}, 1 ≤ *m* ≤ 10^{5})$. Next $*m*$ lines contain pairs of integers. The $*i*$-th line contains numbers $*q*_{*i*}, *w*_{*i*}$ $(1 ≤ *q*_{*i*}, *w*_{*i*} ≤ 10^{5})$.

翻译中…

It is guaranteed that all $*q*_{*i*}$ are distinct.

翻译中…

Output

翻译中…

In a single line print maximum amount of money (in rubles) Sereja can pay.

翻译中…

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

翻译中…

Note

翻译中…

In the first sample Sereja can pay $5$ rubles, for example, if Dima constructs the following array: $[1, 2, 1, 2, 2]$. There are another optimal arrays for this test.

翻译中…

In the third sample Sereja can pay $100$ rubles, if Dima constructs the following array: $[2]$.

翻译中…

样例

Input #1
5 2
1 2
2 3
Output #1
5
InputCopy OutputCopy
Input #2
100 3
1 2
2 1
3 1
Output #2
4
InputCopy OutputCopy
Input #3
1 2
1 1
2 100
Output #3
100
InputCopy OutputCopy
题目信息
题号
367C
时间限制
1 s
内存限制
256 MB
标签
图论
贪心
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。