CodeForces - 367C Sereja and the Arrangement of Numbers (图论&不懂)

CodeForces - 367C
Sereja and the Arrangement of Numbers
Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

Let's call an array consisting of n integer numbers a1a2...anbeautiful 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 aj = x, aj + 1 = y, or aj = y, aj + 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 mcoupons, each contains two integers qi, wi. Coupon i costs wi and allows you to use as many numbers qi as you want when constructing the array a. Values qi 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 wi rubles from Sereja for each qi, 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·106, 1 ≤ m ≤ 105). Next m lines contain pairs of integers. The i-th line contains numbers qi, wi(1 ≤ qi, wi ≤ 105).

It is guaranteed that all qi 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 cincout streams or the%I64d specifier.

Sample Input

Input
5 2
1 2
2 3
Output
5
Input
100 3
1 2
2 1
3 1
Output
4
Input
1 2
1 1
2 100
Output
100

Hint

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].

//读好长时间的题,没读懂,看大神的题意题解还是没理解(太菜了)。

题意:给出m个不同的数,并且每个数都有个费用,现在要在m个数中选择一些数,用这些数组成一个长度为n的数列,并且满足任意两个不同种类的数都相邻。问最大的费用是多少。

思路:不同种数的区别只有费用,不妨按费用从大到小排序,现在就是要在前几个数中选一些满足要求。可以把它想象成一个图,每个数代表一个顶点,两个数相邻代表两个顶点之间有条边,根据题中的要求,构造出来的图就是完全图。做题的时候没想那么多,找了找规律A掉了,AC后看题解才发现这题其实很巧妙。这个数列可以看成一个路径,每在当前数列后加一个数就相当于连了一条边,这样这题就变成了找一条欧拉路径!顶点为奇数的时候一定存在欧拉路径,因为每个顶点的度数都为偶数,顶点为偶数的时候要添加一些边来满足要求,添加的边数即为n/2-1。有趣的题~

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<map>
#include<math.h>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN __int64
#define N 10010
#define M 1000000007
using namespace std;
ll a[N];
int w[N*10];
bool cmp(int x,int y)
{
	return x>y;
}
void init()
{
	for(int i=1;i<N;i++)
	{
		if(i&1)
			a[i]=i*(i-1)/2+1;
		else
			a[i]=i*i/2;
	}
}
int main()
{
	int t,n,m,i,j,k;
	init();
	scanf("%d%d",&n,&m);
	for(i=1;i<=m;i++)
		scanf("%d%d",&t,&w[i]);
	sort(w+1,w+m+1,cmp);
	int p;
	for(i=1;i<N;i++)
	{
		if(a[i]<=n)
			p=i;
		else
			break;
	}
	ll ans=0;
	for(i=1;i<=min(m,p);i++)
		ans+=w[i];
	printf("%lld\n",ans);
	return 0;
}


你可能感兴趣的:(CodeForces - 367C Sereja and the Arrangement of Numbers (图论&不懂))