VJ【规律题】

/*
Description
Let's call an array consisting of n integer numbers a1, a2, ..., an, 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 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 m coupons, 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 cin, cout 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].
*/ 
#include<stdio.h>
#include<stdlib.h>
#include<string.h> 
#include<math.h>
int n, m, seg[2000010], p[100010];
int cmp(const void *a, const void *b)
{
	return *(int *)b - *(int *)a;
}

int main()
{
	int i, j, k;
	while(scanf("%d%d", &n, &m) != EOF)
	{
		k = 0;
		for(i = 0; i < m; ++i)
		{
			scanf("%d%d", &j, &p[i]);
			k += p[i];
		}
		qsort(p, m, sizeof(p[0]), cmp);
		int ans = sqrt(n*2);
		if(ans&1)
		{
			if( (ans-1)*(ans-1)/2 + (ans+1)/2 > n)
			ans--;
		} 
		else
		{
			if( (ans*ans)/2 + (ans+2)/2 <= n)
			ans++;
		}
		if(ans >= m)
		printf("%d\n", k);
		else
		{
			k = 0;
			for(j = 0; j < ans; ++j)
			k += p[j];
			printf("%d\n", k);
		}
	}
	return 0;
} 

//题意:给出一个数组,要求这个数组中的任意2个数必定存在一种相邻的情况 ,这种数组称之为 美丽的数组
//现在给出一个数组的长度,并且给你m个数字,每个数字都是独一无二的,要求你用这些数字来组成美丽数组,每一个数字都有一个价值
//使用某个数需要付出对应的价值,并且该数可以无限使用,求构成该美丽数组需要的最大价值

//思路:对于1个数字美丽数组的长度最短为1  对于2个数字美丽数组的长度最短为2  手推规律
//发现当数字的个数为偶数时 需要构成的美丽数组的最短长度为m*m/2 
//数字的个数为奇数时,美丽数组的长度最短为(m-1)*(m-1)/2 + (m+1)/2
//根据这个规律来求当数组长度 为n时所需要的数字个数,然后求出最大价值 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值