#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

#define MAX 100010

bool paint[MAX][MAX];

void display(int k)
{
	int sum = 1;
	for(int i = 1; i <= k; ++i)
		sum *= 2;
	for(int i = 1; i <= sum; ++i)
	{
		for(int j = 1; j <= sum; ++j)
		{
			if(paint[i][j])
				printf("+");
			else
				printf("*");
		}
		printf("\n");
	}
}

void draw(int sign, int k)
{
	for(int i = 1; i <= sign; ++i)
	{
		for(int j = sign+1; j <= sign*2; ++j)
		{
			if(i <= sign/2)
				paint[i][j] = !paint[i][j-sign];
			else
				paint[i][j] = paint[i][j-sign];
		}
	}
	for(int i = sign+1; i <= sign*2; ++i)
	{
		for(int j = 1; j <= sign; ++j)
			paint[i][j] = paint[i-sign][j];
	}
	for(int i = sign+1; i <= sign*2; ++i)
	{
		for(int j = sign+1; j <= sign*2; ++j)
		{
			if(i <= 3*sign/2)
				paint[i][j] = paint[i-sign][j-sign];
			else
				paint[i][j] = !paint[i-sign][j-sign];
		}
	}
	//cout << sign << endl;
	if(sign == k)
		return;
	else
		draw(sign*2, k);
}

int main()
{
	//freopen("debug\\in.txt", "r", stdin);
	//freopen("CON", "w", stdout);
	int n;
	while(scanf("%d", &n) != EOF)
	{
		for(int i = 0; i < MAX; ++i)
		{
			for(int j = 0; j < MAX; ++j)
				paint[i][j] = ' ';
		}
		paint[1][1] = paint[1][2] = paint[2][1] = 1;
		paint[2][2] = 0;
		int sum = 1;
		for(int i = 1; i <= n; ++i)
			sum *= 2;
		if(n >= 2)
			draw(2, sum);
		display(n);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

#define MAX 1000

bool paint[MAX][MAX];

void display(int k)
{
	int sum = 1;
	for(int i = 1; i <= k; ++i)
		sum *= 2;
	for(int i = 1; i <= sum; ++i)
	{
		for(int j = 1; j <= sum; ++j)
		{
			if(paint[i][j])
				printf("+");
			else
				printf("*");
		}
		printf("\n");
	}
}

void draw(int sign, int k)
{
	for(int i = 1; i <= sign; ++i)
	{
		for(int j = sign+1; j <= sign*2; ++j)
		{
			if(i <= sign/2)
				paint[i][j] = !paint[i][j-sign];
			else
				paint[i][j] = paint[i][j-sign];
		}
	}
	for(int i = sign+1; i <= sign*2; ++i)
	{
		for(int j = 1; j <= sign; ++j)
			paint[i][j] = paint[i-sign][j];
	}
	for(int i = sign+1; i <= sign*2; ++i)
	{
		for(int j = sign+1; j <= sign*2; ++j)
		{
			if(i <= 3*sign/2)
				paint[i][j] = paint[i-sign][j-sign];
			else
				paint[i][j] = !paint[i-sign][j-sign];
		}
	}
	//cout << sign << endl;
	if(sign == k)
		return;
	else
		draw(sign*2, k);
}

int main()
{
	//freopen("debug\\in.txt", "r", stdin);
	//freopen("CON", "w", stdout);
	int n;
	while(scanf("%d", &n) != EOF)
	{
		for(int i = 0; i < MAX; ++i)
		{
			for(int j = 0; j < MAX; ++j)
				paint[i][j] = ' ';
		}
		paint[1][1] = paint[1][2] = paint[2][1] = 1;
		paint[2][2] = 0;
		int sum = 1;
		for(int i = 1; i <= n; ++i)
			sum *= 2;
		if(n >= 2)
			draw(2, sum);
		display(n);
	}
	return 0;
}


//
//  main.cpp
//  CF #337 div2 C
//
//  Created by luras on 15/12/28.
//  Copyright (c) 2015年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 1e5 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

bool a[1010][1010], b[1010][1010];
int k, siz;

int main()
{
    while(~ scanf("%d", &k)){
        if(k == 0) printf("+\n");
        else if(k == 1){
            puts("++");
            puts("+*");
        }
        else{
            ms(a, 0);
            a[2][3] = 1, a[2][4] = 1;
            a[3][2] = 1, a[3][4] = 1;
            a[4][2] = 1, a[4][3] = 1;
            siz = 4;
            for(int i = 1; i <= k - 2; i ++){
                mc(b, a);
                for(int j = 1; j <= siz; j ++){
                    for(int k = 1; k <= siz; k ++){
                        a[j][k] = b[j][k];
                        a[j][k + siz] = b[j][k];
                    }
                }
                for(int j = 1; j <= siz; j ++){
                    for(int k = 1; k <= siz; k ++){
                        a[j + siz][k] = b[j][k];
                        a[j + siz][k + siz] = b[j][k] ^ 1;
                    }
                }
                siz <<= 1;
            }
            for(int i = 1; i <= siz; i ++){
                for(int j = 1; j <= siz; j ++){
                    if(a[i][j]) printf("*");
                    else printf("+");
                }
                printf("\n");
            }
        }
    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


//
//  main.cpp
//  CF #337 div2 C
//
//  Created by luras on 15/12/28.
//  Copyright (c) 2015年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 1e5 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

bool a[1010][1010], b[1010][1010];
int k, siz;

int main()
{
    while(~ scanf("%d", &k)){
        if(k == 0) printf("+\n");
        else if(k == 1){
            puts("++");
            puts("+*");
        }
        else{
            ms(a, 0);
            a[2][3] = 1, a[2][4] = 1;
            a[3][2] = 1, a[3][4] = 1;
            a[4][2] = 1, a[4][3] = 1;
            siz = 4;
            for(int i = 1; i <= k - 2; i ++){
                mc(b, a);
                for(int j = 1; j <= siz; j ++){
                    for(int k = 1; k <= siz; k ++){
                        a[j][k] = b[j][k];
                        a[j][k + siz] = b[j][k];
                    }
                }
                for(int j = 2; j <= siz; j ++){
                    for(int k = 1; k <= siz; k ++){
                        a[siz + j - 1][k] = b[j][k];
                    }
                }
                for(int j = 3; j <= siz; j ++){
                    for(int k = 1; k <= siz; k ++){
                        a[siz + j - 2][k + siz] = b[j][k];
                    }
                }
                for(int k = 1; k <= siz; k ++){
                    a[siz + siz - 1][k + siz] = b[2][k];
                }
                for(int k = 1; k <= siz; k ++){
                    a[siz + siz][k] = b[2][k];
                    a[siz + siz][k + siz] = b[3][k];
                }
                siz <<= 1;
            }
            for(int i = 1; i <= siz; i ++){
                for(int j = 1; j <= siz; j ++){
                    if(a[i][j]) printf("*");
                    else printf("+");
                }
                printf("\n");
            }
        }
    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


//
//  main.cpp
//  CF #337 div2 C
//
//  Created by luras on 15/12/28.
//  Copyright (c) 2015年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 1e5 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

bool a[1010][1010], b[1010][1010];
int k, siz;

int main()
{
    while(~ scanf("%d", &k)){
        if(k == 0) printf("+\n");
        else if(k == 1){
            puts("++");
            puts("+*");
        }
        else{
            ms(a, 0);
            a[2][3] = 1, a[2][4] = 1;
            a[3][2] = 1, a[3][4] = 1;
            a[4][2] = 1, a[4][3] = 1;
            siz = 4;
            for(int i = 1; i <= k - 2; i ++){
                mc(b, a);
                for(int j = 1; j <= siz; j ++){
                    for(int k = 1; k <= siz; k ++){
                        a[j][k] = b[j][k];
                        a[j + siz][k] = b[j][k];
                        a[j][k + siz] = b[j][k];
                    }
                }
                for(int j = 2; j <= siz; j ++){
                    for(int k = 1; k <= siz; k ++){
                        a[siz + j - 1][k + siz] = b[j][k];
                    }
                }
                for(int k = 1; k <= siz; k ++){
                    a[siz + siz][k + siz] = b[2][k];
                }
                siz <<= 1;
            }
            for(int i = 1; i <= siz; i ++){
                for(int j = 1; j <= siz; j ++){
                    if(a[i][j]) printf("*");
                    else printf("+");
                }
                printf("\n");
            }
        }
    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


//
//  main.cpp
//  CF #337 div2 C
//
//  Created by luras on 15/12/28.
//  Copyright (c) 2015年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 1e5 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

bool a[1010][1010], b[1010][1010];
int k, siz;

int main()
{
    while(~ scanf("%d", &k)){
        if(k == 0) printf("+\n");
        else if(k == 1){
            puts("++");
            puts("+*");
        }
        else{
            ms(a, 0);
            a[2][3] = 1, a[2][4] = 1;
            a[3][2] = 1, a[3][4] = 1;
            a[4][2] = 1, a[4][3] = 1;
            siz = 4;
            for(int i = 1; i <= k - 2; i ++){
                mc(b, a);
                for(int j = 1; j <= siz; j ++){
                    for(int k = 1; k <= siz; k ++){
                        a[j][k] = b[j][k];
                        a[j + siz][k] = b[j][k];
                        a[j][k + siz] = b[j][k];
                    }
                }
                for(int j = 2; j <= siz; j ++){
                    for(int k = 1; k <= siz; k ++){
                        a[siz + j - 1][k + siz] = b[j][k];
                    }
                }
                for(int k = 1; k <= siz; k ++){
                    a[siz + siz][k + siz] = b[1][k];
                }
                siz <<= 1;
            }
            for(int i = 1; i <= siz; i ++){
                for(int j = 1; j <= siz; j ++){
                    if(a[i][j]) printf("*");
                    else printf("+");
                }
                printf("\n");
            }
        }
    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


//
//  main.cpp
//  CF #337 div2 C
//
//  Created by luras on 15/12/28.
//  Copyright (c) 2015年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 1e5 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

bool a[260][260], b[260][260];
int k, siz;

int main()
{
    while(~ scanf("%d", &k)){
        if(k == 0) printf("+\n");
        else if(k == 1){
            puts("++");
            puts("+*");
        }
        else{
            ms(a, 0);
            a[2][3] = 1, a[2][4] = 1;
            a[3][2] = 1, a[3][4] = 1;
            a[4][2] = 1, a[4][3] = 1;
            siz = 4;
            for(int i = 1; i <= k - 2; i ++){
                mc(b, a);
                for(int j = 1; j <= siz; j ++){
                    for(int k = 1; k <= siz; k ++){
                        a[j][k] = b[j][k];
                        a[j + siz][k] = b[j][k];
                        a[j][k + siz] = b[j][k];
                    }
                }
                for(int j = 2; j <= siz; j ++){
                    for(int k = 1; k <= siz; k ++){
                        a[siz + j - 1][k + siz] = b[j][k];
                    }
                }
                for(int k = 1; k <= siz; k ++){
                    a[siz + siz][k + siz] = b[1][k];
                }
                siz <<= 1;
            }
            for(int i = 1; i <= siz; i ++){
                for(int j = 1; j <= siz; j ++){
                    if(a[i][j]) printf("*");
                    else printf("+");
                }
                printf("\n");
            }
        }
    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


//IN THE NAME OF ALLAH
//یا صاحب الزمان
#include <bits/stdc++.h>
using namespace std;

const int maxn = 600;
int n, ghab, jad;
bool ans[maxn][maxn];

int main()
{
    cin >> n;
    if (n == )
        return cout << "*", 0;
    ans[0][0] = 1;
    for (int i = 0; i < n; i++){
        ghab = 1 << i;
        jad = 1 << (i + 1);
        for (int j = 0; j < ghab; j++)
            for (int t = ghab; t < jad; t++)
                ans[j][t] = ans[j][t - ghab];
        for (int j = ghab; j < jad; j++)
            for (int t = 0; t < ghab; t++)
                ans[j][t] = ans[j - ghab][t];
        for (int j = ghab; j < jad; j++)
            for (int t = ghab; t < jad; t++)
                ans[j][t] = 1 - ans[j - ghab][t];
    }
    for (int i = 0; i < jad; i++, cout << "\n")
        for (int j = 0; j < jad; j++)
            cout << (ans[i][j] ? '*' : '+');
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.14159265359
#define int long long
#define pii pair<int, int>
#define ld long double
const int mod = 1e9 + 7;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);

  #ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
  #endif

   int n,m;
   cin>>n>>m;
   vector<pii>v(m);
   for(int i=0;i<m;i++)
   {
     cin>>v[i].second>>v[i].first;
   }

   sort(v.rbegin(),v.rend());

    int ans=v[0].first ;
    for (int k=2;k<=m;k++)
    {
        if (max((k*(k-1))/2+1,k*(k/2))<=n)
            ans+=v[k-1].first;
        else
            break;
    }
    cout<<ans<<"\n";

}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.14159265359
#define int long long
#define pii pair<int, int>
#define ld long double
const int mod = 1e9 + 7;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);

  #ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
  #endif

   int n,m;
   cin>>n>>m;
   vector<pii>v(m);
   for(int i=0;i<m;i++)
   {
     cin>>v[i].second>>v[i].first;
   }

   sort(v.begin(),v.end(),greater<pii>());

   int x=min(n-1,m);
   int ans=0;
   for(int i=0;i<x;i++)
   {
     ans+=v[i].first;
   }
   if(n==1)cout<<v[0].first<<"\n";
   else
   cout<<ans<<"\n";

}


#include <bits/stdc++.h>

using namespace std;

int main(){
    int N, M;
    cin >> N >> M;
    priority_queue<int> pq;
    for(int i = 1; i<=M; i++){
        int q, w;
        cin >> q >> w;
        pq.push(w);
    }
    int n = 0;
    while(true){
        if(n%2){
            if(n*(n-1)/2+1 > N){
                break;
            }
            else{
                n++;
            }
        }
        else{
            if(n*(n-1)/2+(n-2)/2+1 > N){
                break;
            }
            else{
                n++;
            }
        }
    }
    n--;
    long long ans = 0;
    while(n-- && pq.size()){
        ans += pq.top();
        pq.pop();
    }
    cout << ans << endl;
}


/*
@author: sharrad99
*/
#include<bits/stdc++.h>
using namespace std;
int main(){

	#ifndef ONLINE_JUDGE
	freopen("C:\\Users\\sharr\\Documents\\Input.txt", "r", stdin);
	freopen("C:\\Users\\sharr\\Documents\\Output.txt", "w", stdout);
	#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int64_t n, m;
	cin >> n >> m;

	vector<int64_t> c(m);
	for(int64_t i = 0; i < m; i++) {
		int64_t q, w;
		cin >> q >> w;
		c[i] = w;
	}
	sort(c.rbegin(), c.rend());

	int64_t res = 0;
	for(int64_t i = 1, s; i <= m; i++) {
		if(i & 1) {
			s = ((i * (i - 1)) / 2);
		}
		else {
			s = ((i * (i - 1)) / 2) + ((i / 2) - 1);
		}

		if(s >= n) break;
		res += c[i - 1];
	}
	cout << res << '\n';
}


/*
@author: sharrad99
*/
#include<bits/stdc++.h>
using namespace std;
int main(){

	#ifndef ONLINE_JUDGE
	freopen("C:\\Users\\sharr\\Documents\\Input.txt", "r", stdin);
	freopen("C:\\Users\\sharr\\Documents\\Output.txt", "w", stdout);
	#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int64_t n, m;
	cin >> n >> m;

	vector<int64_t> c(m);
	for(int64_t i = 0; i < m; i++) {
		int64_t q, w;
		cin >> q >> w;
		c[i] = w;
	}
	sort(c.rbegin(), c.rend());

	int64_t res = 0;
	for(int64_t i = 0; i < m; i++) {
		int64_t s = ((i + 1) * i) / 2;
		if(!(i & 1)) {
			s += (i + 1) / 2 - 1;
		}
		if(s >= n) {
			break;
		}
		else {
			res += c[i];
		}
	}
	cout << res << '\n';
}


#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long

const int N=1e5+5;

int n, m, temp, ans=0;
int a[N];

int check(int cur)
{
	if(cur%2)
		return (cur*(cur-1)/2 + 1)<=n;
	else
		return (cur*(cur-1)/2 + cur/2)<=n;
}

int32_t main()
{
	IOS;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
		cin>>temp>>a[i];
	sort(a+1, a+m+1);
	reverse(a+1, a+m+1);
	for(int i=1;i<=m;i++)
	{
		if(check(i))
			ans+=a[i];
		else
			break;
	}
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int, int >
#define F first
#define S second
#define int  long long int
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
using namespace std;
/// age ye mosh dp ro baze dashti dp tedad baz shodeye baste nashode yadet nare !!
/// joooob zadam
const int N=2e5+100;

int32_t main(){
    sync;
    vector <int> a;
    ll n;
    cin >> n;
    ll k=0;
    cin >> k;
    for (int i=0;i<k;i++){
        ll y;
    cin >> y;
        ll x;
        cin >> x;
        a.pb(x);
    }
    ll cnt=0;
    for (int i=1;i<=k;i++){
        ll z=(i*(i-1))/2;
        if (i%2==0){
            z+=i/2-1;
        }
        if (z<n){
            cnt=i;
        }
    }
    sort(a.begin(),a.end());
    reverse(a.begin(),a.end());
    ll ans=0;
    for (int i=0;i<cnt;i++){
        ans+=a[i];
    }
    cout << ans;

}


#include <bits/stdc++.h>
using namespace std;
#define sc(x)            scanf("%d",&x)
#define scc(x)           scanf("%c",&x)
#define scl(x)           scanf("%lld",&x)
#define sz(v)	     	(v.size())
#define mem(v, d)		memset(v, d, sizeof(v))
#define oo				2000000100
#define OO				2000000000000000100
#define PI 3.14159265
#define S second
#define F first
#define Ceil(x,y) ((x+y-1)/y)
#define EPS 1e-8
#define IO ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
const double pi=3.14159265358979323846264338327950288419716939937510582097494459;
typedef unsigned int uint;
typedef long long ll;
int dX[] = {0,0,1,-1,0};
int dY[] = {1,-1,0,0,0};
string alpha = "abcdefghijklmnopqrstuvwxyz";
const int N = 100100;
const int MOD = 1e9 + 7;
int n,m,arr[N];
bool ok(ll r){
    return (r/2LL*r + (r%2) <= n);
}
int main()
{
#ifndef ONLINE_JUDGE
    //freopen("in.txt","r",stdin);
#else
    //freopen("journey.in","r",stdin);
    //freopen("journey.out","w",stdout);
#endif
    sc(n); sc(m);
    int _;
    for(int i = 0;i<m;++i)
        sc(_),sc(arr[i]);

    sort(arr,arr+m);
    reverse(arr,arr+m);
    ll out = 0;
    for(int i = 0;i<m;++i){
        if(ok(i+1)) out += arr[i];
    }
    cout << out << endl;

    return 0;

}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;

struct maca {long long int x,v;} mg[100005];
int poredi(maca a,maca b)
{
    if(a.v>b.v) return 1;
    else return 0;
}
long long int ceo(long long int a,long long int b)  {return (a+b-1)/b;}
int moze(long long int r,long long int n)
{
    long long int x,y;
    x=2*ceo(r,2)+(r-2)*ceo(r-1,2);
    y=ceo(r+1,2)+(r-1)*ceo(r-1,2);
    if(x<=n || y<=n) return 1;
    return 0;
}
int main()
{
    long long int n,m,i,j,k,a,b,res=0,r;
    cin>>n>>m;
    for(i=1;i<=m;i++) cin>>mg[i].x>>mg[i].v;
    sort(mg+1,mg+m+1,poredi);
    for(i=1;i<=sqrt(10*n);i++)
    if(moze(i,n)==0) {r=i-1; break;}
    for(i=1;i<=r;i++) res+=mg[i].v;
    cout<<res<<endl;
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;

struct maca {long long int x,v;} mg[100005];

int poredi(maca a,maca b)
{
    if(a.v>b.v) return 1;
    else return 0;
}

long long int ceo(long long int a,long long int b)
{
    return (a+b-1)/b;
}

int moze(long long int r,long long int n)
{
    long long int x,y;

    x=2*ceo(r,2)+(r-2)*ceo(r-1,2);

    y=ceo(r+1,2)+(r-1)*ceo(r-1,2);

    //if(r==2) cout<<x<<" "<<y<<endl;

    if(x<=n || y<=n) return 1;
    else return 0;
}

int main()
{
    long long int n,m,i,j,k,a,b,res=0,r;
    cin>>n>>m;

    for(i=1;i<=m;i++) cin>>mg[i].x>>mg[i].v;

    sort(mg+1,mg+m+1,poredi);

    //razunamo r

    for(i=1;i<=sqrt(10*n);i++)
    {
        if(moze(i,n)==0) {r=i-1; break;}
    }

    //racunamo r

    for(i=1;i<=r;i++) res+=mg[i].v;

    cout<<res<<endl;

    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;

struct maca {long long int x,v;} mg[100005];

int poredi(maca a,maca b)
{
    if(a.v>b.v) return 1;
    else return 0;
}

long long int ceo(long long int a,long long int b)
{
    return (a+b-1)/b;
}

int moze(long long int r,long long int n)
{
    long long int x,y;

    x=2*ceo(r,2)+(r-2)*ceo(r-1,2);

    y=ceo(r+1,2)+(r-1)*ceo(r-1,2);

    if(x<=n || y<=n) return 1;
    else return 0;
}

int main()
{
    long long int n,m,i,j,k,a,b,res=0,r;
    cin>>n>>m;

    for(i=1;i<=m;i++) cin>>mg[i].x>>mg[i].v;

    sort(mg+1,mg+m+1,poredi);

    //razunamo r

    for(i=1;i<=sqrt(4*n);i++)
    {
        if(moze(i,n)==0) {r=i-1; break;}
    }

    //racunamo r

    for(i=1;i<=r;i++) res+=mg[i].v;

    cout<<res<<endl;

    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;

struct maca {long long int x,v;} mg[100005];
int poredi(maca a,maca b)
{
    if(a.v>b.v) return 1;
    else return 0;
}
long long int ceo(long long int a,long long int b)  return (a+b-1)/b;
int moze(long long int r,long long int n)
{
    long long int x,y;
    x=2*ceo(r,2)+(r-2)*ceo(r-1,2);
    y=ceo(r+1,2)+(r-1)*ceo(r-1,2);
    if(x<=n || y<=n) return 1;
    return 0;
}
int main()
{
    long long int n,m,i,j,k,a,b,res=0,r;
    cin>>n>>m;
    for(i=1;i<=m;i++) cin>>mg[i].x>>mg[i].v;
    sort(mg+1,mg+m+1,poredi);
    for(i=1;i<=sqrt(10*n);i++)
    if(moze(i,n)==0) {r=i-1; break;}
    for(i=1;i<=r;i++) res+=mg[i].v;
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n, m;
	cin >> n >> m;
	vector <int> w(m);
	for (auto & i : w) {
		int x;
		cin >> x >> i;
	}

	sort(w.rbegin(), w.rend());

	auto l_min = [](int x) -> long long {
		if (x % 2 == 1)
			return 1LL * x * (x - 1) / 2 + 1;
		else
			return 1LL * x * (x - 1) / 2 + 1 + (x - 2) / 2;
	};

	long long ans = 0, s_act = 0;
	for (int i = 0; i < (int)w.size(); i++) {
		s_act += w[i];
		if (l_min(i + 1) <= n)
			ans = s_act;
	}

	cout << ans << '\n';
	return 0;
}


//Sereja and the Arrangement of Numbers
//6/5/20

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
long long N, M;
vector<pair<int,int>> wq;

int main(int argc, const char * argv[]) {
    cin >> N >> M;
    for (int i = 0, w, q; i < M; i++ ){cin >> q >> w; wq.push_back(ii(w,q));}
    sort(wq.begin(), wq.end(), greater<ii>());

    int idx = 1;
    for (; idx <= N+1; idx++){
        long long  amt = 0;
        if (idx%2 == 1) amt = ((long long)idx*(long long)(idx-1)/(long long) 2)+1;
        else amt = ((long long)idx*(long long)(idx-1)/(long long)2)+idx/2+1;
//        cout<<"idx "<<idx<<" amt "<<amt<<endl;
        if (N < amt){
            idx--;
//            cout<<"yay"<<endl;
            break;
        }
    }

    long long ANS = 0;
//    cout<<"final index "<<idx<<endl;
    for (long long i = 0; i < min((int) wq.size(), idx); i++){
        ANS += (long long) wq[i].first;
    }
    cout<<ANS<<endl;
    return 0;
}


//Sereja and the Arrangement of Numbers
//6/5/20

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
int N, M;
vector<pair<int,int>> wq;

int main(int argc, const char * argv[]) {
    cin >> N >> M;
    for (int i = 0, w, q; i < M; i++ ){cin >> q >> w; wq.push_back(ii(w,q));}
    sort(wq.begin(), wq.end(), greater<ii>());

    int idx = 1;
    for (; idx <= N+1; idx++){
        int amt = 0;
        if (idx%2 == 1) amt = (idx*(idx-1)/2)+1;
        else amt = (idx*(idx-1)/2)+idx/2+1;
//        cout<<"idx "<<idx<<" amt "<<amt<<endl;
        if (N < amt){
            idx--;
//            cout<<"yay"<<endl;
            break;
        }
    }

    long long ANS = 0;
//    cout<<"final index "<<idx<<endl;
    for (int i = 0; i < min((int)wq.size(), idx); i++){
        ANS += (long long) wq[i].first;
    }
    cout<<ANS<<endl;
    return 0;
}


//Sereja and the Arrangement of Numbers
//6/5/20

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
long long N, M;
vector<pair<int,int>> wq;

int main(int argc, const char * argv[]) {
    cin >> N >> M;
    for (int i = 0, w, q; i < M; i++ ){cin >> q >> w; wq.push_back(ii(w,q));}
    sort(wq.begin(), wq.end(), greater<ii>());

    int idx = 1;
    for (; idx <= N+1; idx++){
        long long  amt = 0;
        if (idx%2 == 1) amt = ((long long)idx*(long long)(idx-1)/(long long) 2)+1;
        else amt = ((long long)idx*(long long)(idx-1)/(long long)2)+idx/2;
//        cout<<"idx "<<idx<<" amt "<<amt<<endl;
        if (N < amt){
            idx--;
//            cout<<"yay"<<endl;
            break;
        }
    }

    long long ANS = 0;
//    cout<<"final index "<<idx<<endl;
    for (long long i = 0; i < min((int) wq.size(), idx); i++){
        ANS += (long long) wq[i].first;
    }
    cout<<ANS<<endl;
    return 0;
}


//Sereja and the Arrangement of Numbers
//6/5/20

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
int N, M;
vector<pair<int,int>> wq;

int main(int argc, const char * argv[]) {
    cin >> N >> M;
    for (int i = 0, w, q; i < M; i++ ){cin >> q >> w; wq.push_back(ii(w,q));}
    sort(wq.begin(), wq.end(), greater<ii>());

    int idx = 1;
    for (; idx <= N+1; idx++){
        int amt = 0;
        if (idx%2 == 1) amt = (idx*(idx-1)/2)+1;
        else amt = (idx*(idx-1)/2)+idx/2+1;
//        cout<<"idx "<<idx<<" amt "<<amt<<endl;
        if (N < amt){
            idx--;
//            cout<<"yay"<<endl;
            break;
        }
    }

    int ANS = 0;
//    cout<<"final index "<<idx<<endl;
    for (int i = 0; i < min((int)wq.size(), idx); i++){
        ANS += wq[i].first;
    }
    cout<<ANS<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define ll long long
#define maxheap priority_queue<int>
#define minheap priority_queue<int,vector<int>,greater<int>>
const  long double eps = 1e-9;
const long double pi = acos(-1.0);
using namespace std;

int m;
ll n;

void solve(){
    cin >> n >> m;
    vector <int> ans;

    for (int i = 1; i <= m; i++){
        int p, w;
        cin >> p >> w;
        ans.pb(w);
    }
    sort(ans.begin(), ans.end());
    reverse(ans.begin(), ans.end());
    int ind = 0;
    for (int i = 1; i <= m; i++){
        ll x = i;
        if (x % 2 == 0) x = x * (x - 1) / 2 + 1;
        else x = x * (x - 1) / 2 + x / 2;
        if (x <= n) ind = i;
    }
    ll sum = 0;
    for (int i = 0; i < ind; i++) sum += ans[i];
    cout << sum << endl;
}

int main() {
    ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
    cout.setf(ios::fixed), cout.precision(10);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int step;
    step = 1;
    //cin >> step;
    for (int i = 1; i <= step; i++){
         solve();
    }
}


#include<bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define ll long long
#define maxheap priority_queue<int>
#define minheap priority_queue<int,vector<int>,greater<int>>
const  long double eps = 1e-9;
const long double pi = acos(-1.0);
using namespace std;

int m;
ll n;
bool check(ll x){
    if (x % 2 == 0)
        return (x * (x - 1) / 2 + 1 <= n);
    else
        return (x * (x - 1) / 2 + x / 2 <= n);

}
void solve(){
    cin >> n >> m;
    vector <int> ans;

    for (int i = 1; i <= m; i++){
        int p, w;
        cin >> p >> w;
        ans.pb(w);
    }
    sort(ans.begin(), ans.end());
    reverse(ans.begin(), ans.end());
    int l = 0;
    int r = m + 1;
    while (r - l > 1){
        int mid = (l + r) >> 1;
        if (check(mid)) l = mid;
        else r = mid;
    }
    ll sum = 0;
    for (int i = 0; i < l; i++)
        sum += ans[i];
    cout << sum << endl;
}

int main() {
    ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
    cout.setf(ios::fixed), cout.precision(10);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int step;
    step = 1;
    //cin >> step;
    for (int i = 1; i <= step; i++){
         solve();
    }
}


#include<bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define ll long long
#define maxheap priority_queue<int>
#define minheap priority_queue<int,vector<int>,greater<int>>
const  long double eps = 1e-9;
const long double pi = acos(-1.0);
using namespace std;

int m;
ll n;

void solve(){
    cin >> n >> m;
    vector <int> ans;

    for (int i = 1; i <= m; i++){
        int p, w;
        cin >> p >> w;
        ans.pb(w);
    }
    sort(ans.begin(), ans.end());
    reverse(ans.begin(), ans.end());
    int ind = 0;
    for (int i = 1; i <= m; i++){
        ll x = i;
        if (x % 2 == 0) x = x * (x - 1) / 2 + 1;
        else x = x * (x - 1) / 2 + x / 2 - 1;
        if (x <= n) ind = i;
    }
    ll sum = 0;
    for (int i = 0; i < ind; i++) sum += ans[i];
    cout << sum << endl;
}

int main() {
    ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
    cout.setf(ios::fixed), cout.precision(10);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int step;
    step = 1;
    //cin >> step;
    for (int i = 1; i <= step; i++){
         solve();
    }
}


#include<bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define ll long long
#define maxheap priority_queue<int>
#define minheap priority_queue<int,vector<int>,greater<int>>
const  long double eps = 1e-9;
const long double pi = acos(-1.0);
using namespace std;

int m;
ll n;

void solve(){
    cin >> n >> m;
    vector <int> ans;

    for (int i = 1; i <= m; i++){
        int p, w;
        cin >> p >> w;
        ans.pb(w);
    }
    sort(ans.begin(), ans.end());
    reverse(ans.begin(), ans.end());
    int ind = 0;
    for (int i = 1; i <= m; i++){
        ll x = i;
        if (i % 2 == 0)
            x = x * (x - 1) / 2 + (x - 2) / 2 + 1;
        else
            x = x * (x - 1) / 2 + 1;
        if (x <= n) ind = i;

    }
    ll sum = 0;
    for (int i = 0; i < ind; i++) sum += ans[i];
    cout << sum << endl;
}

int main() {
    ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
    cout.setf(ios::fixed), cout.precision(10);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int step;
    step = 1;
    //cin >> step;
    for (int i = 1; i <= step; i++){
         solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
#define forr(x,n) for(int x = 0; x < n; ++x)
#define pr pair<int,int>
#define ll long long int
const int mod=1e9+7;
const int inf=1e9+7;
ll f(ll n) // directly calculated from condition of existence of euler path
{
	ll ans;
	if(n<=2)
	return n;
	if(n%2==1)
	ans=n*(n-1)/2+1;
	else
	ans=n*(n-1)/2+n/2-1+1;
	return ans;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n,m;
    cin>>n>>m;
    ll dp[m+1];
	// dont fall in trap of semi euler thing as mentioned in solution....just do euler path with vertex as distinct numbers
    // dp[i]=... with i distict digit...minimum number of positions needed... //
	//find euler path in i-regular graph...vertex=distict numbers....edge between 1 and 2 means in euler path 2 comes just after one
	//if even number of vertex then add one extra edge between 1-2,3-4,5-6...leaving one pair
    forr(i,m+1)
    dp[i]=f(i);
    ll x,cost[m];
    forr(i,m)
    cin>>x>>cost[i];
    sort(cost,cost+m,greater<int>());
    // find maximum i such that dp[i]<=n
	int i;
    for(i=m;i>=0;i--)
    {
    	if(dp[i]<=n)
    	break;
	}
//	cout<<dp[i]<<" "<<dp[i+1]<<endl;
	ll sum=0;
	forr(j,i)
	sum+=cost[j];
	cout<<sum;

}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

int main() {
    ios_base::sync_with_stdio(0);
    cout.tie(0); cin.tie(0);
    int n, k;
    cin >> n >> k;
    int ans = 0;
    for (int i = 1; i <= k; i++) {
        if (i * (i - 1) / 2 + (i / 2 - 1) * (i % 2 == 0) + 1 <= n) {
            ans = i;
        }
    }
    vector<pair<int, int>> v(k);
    for (int i = 0; i < k; i++) {
        cin >> v[i].second >> v[i].first;
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    ll sum = 0;
    for (int i = 0; i < ans; i++) {
        sum += v[i].first;
    }
    cout << sum;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

int main() {
    ios_base::sync_with_stdio(0);
    cout.tie(0); cin.tie(0);
    ll n, k;
    cin >> n >> k;
    int ans = 0;
    for (ll i = 1; i <= k; i++) {
        if (i * (i - 1) / 2 + (i / 2 - 1) * (i % 2 == 0) + 1 <= n) {
            ans = i;
        }
    }
    vector<pair<int, ll>> v(k);
    for (int i = 0; i < k; i++) {
        cin >> v[i].second >> v[i].first;
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    ll sum = 0;
    for (int i = 0; i < ans; i++) {
        sum += v[i].first;
    }
    cout << sum;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;

const int N = (int) 1e5 + 100;

struct edge {
	int to;
	bool fict;

	edge(int to, bool fict) : to(to), fict(fict) {}
};

vector<edge> g[N];
set<pii> used;

int path_len;

void dfs(int v) {
	while (!g[v].empty()) {
		edge e = g[v].back();
		g[v].pop_back();

		if (used.count(pii(v, e.to)) || used.count(pii(e.to, v))) {
			continue;
		}

		path_len++;

		if (e.fict) {
			path_len++;
		}

		used.emplace(v, e.to);
		dfs(e.to);
	}
}

ll countGraphSize(ll n) {
	if (n % 2) {
		return n * (n - 1) / 2 + 1;
	} else {
		return n * (n - 1) / 2 + n / 2;
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n, k;
	cin >> n >> k;

	vector<pii> a(k);

	for (pii &p : a) {
		cin >> p.second >> p.first;
	}

	sort(a.rbegin(), a.rend());

	while (countGraphSize(a.size()) > n) {
		a.pop_back();
	}

	ll sum = 0;

	for (auto p : a) {
		sum += p.first;
	}

	cout << sum;

	return 0;
}


#include<bits/stdc++.h>
#define int long long int
using namespace std;
int n,m;
vector<pair<int,int>>p;
map<int,int>mm;
vector<int>a;
main() {ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>m;
for(int i=0;i<m;i++){
  int x,y;
  cin>>x>>y;
  p.push_back({x,y});
  mm[x]=y;
  a.push_back(y);
}
   sort(a.begin(),a.end());
   reverse(a.begin(),a.end());
   if(n==1){
    cout<<a[0];return 0;
   }
   for(int i=1;i<m;i++)a[i]+=a[i-1];
  for(int i=m;i>=1;i--){
    int x;
    if(m%2==0){
      x=(n*(n-1))/2+(n/2);
    }
    else x=(n*(n-1))/2;
    if(x<=n){
      cout<<a[i-1];return 0;
    }
  }
}


#include<bits/stdc++.h>
#define int long long int
using namespace std;
int n,m;
vector<int>a;
main() {ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>m;
for(int i=0;i<m;i++){
  int x,y;
  cin>>x>>y;
  a.push_back(y);
}
   sort(a.begin(),a.end());
   reverse(a.begin(),a.end());
   if(n==1){
    cout<<a[0];return 0;
   }
   for(int i=1;i<m;i++)a[i]+=a[i-1];
  for(int i=m;i>=1;i--){
    int x;
    if(i%2==0){
      x=(i*(i-1))/2+(i/2);
    }
    else x=(i*(i-1))/2+1;
    //cout<<x<<"    ";
    if(x<=n){
      cout<<a[i-1];return 0;
    }
  }

}


#include<bits/stdc++.h>
#define int long long int
using namespace std;
int n,m;
vector<pair<int,int>>p;
map<int,int>mm;
vector<int>a;
main() {ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>m;
for(int i=0;i<m;i++){
  int x,y;
  cin>>x>>y;
  p.push_back({x,y});
  mm[x]=y;
  a.push_back(y);
}
   sort(a.begin(),a.end());
   reverse(a.begin(),a.end());
   if(n==1){
    cout<<a[0];return 0;
   }
   for(int i=1;i<m;i++)a[i]+=a[i-1];
  for(int i=m;i>=1;i--){
    int x;
    if(i%2==0){
      x=(i*(i-1))/2+(i/2);
    }
    else x=(i*(i-1))/2;
    //cout<<x<<"    ";
    if(x<=n){
      cout<<a[i-1];return 0;
    }
  }

}


#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define int long long
#define ld long double
#define fr first
#define sc second
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define umap unordered_map<ll,ll>
#define prq priority_queue<int>
#define inf 1e18
#define test cout<<"abcd\n";
#define show1(x) cout<<x<<"\n"
#define show2(x,y) cout<<x<<" "<<y<<"\n"
#define show3(x,y,z) cout<<x<<" "<<y<<" "<<z<<"\n"
#define endl "\n"
#define mod (int)1000000007
#define N (int)1e5+5
int n, m, temp, ans=0;
int a[N];
int solve(int val){
	if(val%2)
		return (val*(val-1)/2 + 1)<=n;
	else
		return (val*(val-1)/2 + val/2)<=n;
}
int32_t main(){
	fast;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
		cin>>temp>>a[i];
	sort(a+1, a+m+1);
	reverse(a+1, a+m+1);
	for(int i=1;i<=m;i++){
		if(solve(i))
			ans+=a[i];
		else
			break;
	}
	cout<<ans;
	return 0;
}


/* be name khoda */

// #define stream_enable
#define long_enable
#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;

#ifdef stream_enable
#define cin sss
#endif
#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;

const ll MOD = 1000000007;
const long long BIG = 1446803456761533460;
const int Big = 336860180;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()

#define debug(x) cout << #x << " -> " << (x) << endl
#define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl
#define debugp(x) cout << #x << " -> " << "(" << (x).F << ", " << (x).S << ")" << endl
#define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl
#define debugap(x, n) cout << #x << " ->\n"; fori (i1_dap, n) { cout << "(" << (x)[i1_dap].F << ", " << (x)[i1_dap].S << ")\n"; } cout << endl
#define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl
#define debugav(x, n) cout << #x << " ->\n"; fori (i1_dav, n) { fori (i2_dav, (x)[i1_dav].size()) { cout << (x)[i1_dav][i2_dav] << ' '; } cout << '\n'; } cout << endl
#define debugia(x, n) cout << #x << " ->\n"; fori (i1_dia, n) { cout << i1_dia << " : " << (x)[i1_dia] << '\n'; } cout << endl

#define forifrom(i, s, n) for(ll i = (s); i < (n); ++i)
#define forirto(i, n, e) for(ll i = (n) - 1; i >= (e); --i)
#define fori(i, n) forifrom (i, 0, n)
#define forir(i, n) forirto (i, n, 0)

#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define Add(a, b) a = ((a) + (b)) % MOD
#define Mul(a, b) a = (1LL * (a) * (b)) % MOD

stringstream sss;

// -----------------------------------------------------------------------

const ll maxn = 100010;

ll n, m;
ll W[maxn];

void MAIN() {

    cin >> n >> m;
    fori (i, m) cin >> W[i] >> W[i];
    sort(W, W + m, greater<ll>());
    ll l = 1, r = 3000;
    while (r - l > 1) {
        ll t = (l + r) / 2;
        ll val = t % 2 ? t * (t - 1) / 2 + 1 : t * (t + 1) / 2 - 2;
        if (val > n) r = t;
        else l = t;
    }
    smin(l, m);
    ll s = 0;
    fori (i, l) s += W[i];
    cout << s << '\n';

}

// -----------------------------------------------------------------------

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    sss << R"(

    )";
    MAIN();
    return 0;
}


/* be name khoda */

// #define stream_enable
#define long_enable
#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;

#ifdef stream_enable
#define cin sss
#endif
#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;

const ll MOD = 1000000007;
const long long BIG = 1446803456761533460;
const int Big = 336860180;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()

#define debug(x) cout << #x << " -> " << (x) << endl
#define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl
#define debugp(x) cout << #x << " -> " << "(" << (x).F << ", " << (x).S << ")" << endl
#define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl
#define debugap(x, n) cout << #x << " ->\n"; fori (i1_dap, n) { cout << "(" << (x)[i1_dap].F << ", " << (x)[i1_dap].S << ")\n"; } cout << endl
#define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl
#define debugav(x, n) cout << #x << " ->\n"; fori (i1_dav, n) { fori (i2_dav, (x)[i1_dav].size()) { cout << (x)[i1_dav][i2_dav] << ' '; } cout << '\n'; } cout << endl
#define debugia(x, n) cout << #x << " ->\n"; fori (i1_dia, n) { cout << i1_dia << " : " << (x)[i1_dia] << '\n'; } cout << endl

#define forifrom(i, s, n) for(ll i = (s); i < (n); ++i)
#define forirto(i, n, e) for(ll i = (n) - 1; i >= (e); --i)
#define fori(i, n) forifrom (i, 0, n)
#define forir(i, n) forirto (i, n, 0)

#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define Add(a, b) a = ((a) + (b)) % MOD
#define Mul(a, b) a = (1LL * (a) * (b)) % MOD

stringstream sss;

// -----------------------------------------------------------------------

const ll maxn = 100010;

ll n, m;
ll W[maxn];

void MAIN() {

    cin >> n >> m;
    fori (i, m) cin >> W[i] >> W[i];
    sort(W, W + m, greater<ll>());
    ll l = 1, r = 3000;
    while (r - l > 1) {
        ll t = (l + r) / 2;
        ll val = t % 2 ? t * (t - 1) / 2 + 1 : t * t / 2;
        if (val > n) r = t;
        else l = t;
    }
    smin(l, m);
    ll s = 0;
    fori (i, l) s += W[i];
    cout << s << '\n';

}

// -----------------------------------------------------------------------

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    sss << R"(

    )";
    MAIN();
    return 0;
}


/* be name khoda */

// #define stream_enable
#define long_enable
#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;

#ifdef stream_enable
#define cin sss
#endif
#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;

const ll MOD = 1000000007;
const long long BIG = 1446803456761533460;
const int Big = 336860180;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()

#define debug(x) cout << #x << " -> " << (x) << endl
#define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl
#define debugp(x) cout << #x << " -> " << "(" << (x).F << ", " << (x).S << ")" << endl
#define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl
#define debugap(x, n) cout << #x << " ->\n"; fori (i1_dap, n) { cout << "(" << (x)[i1_dap].F << ", " << (x)[i1_dap].S << ")\n"; } cout << endl
#define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl
#define debugav(x, n) cout << #x << " ->\n"; fori (i1_dav, n) { fori (i2_dav, (x)[i1_dav].size()) { cout << (x)[i1_dav][i2_dav] << ' '; } cout << '\n'; } cout << endl
#define debugia(x, n) cout << #x << " ->\n"; fori (i1_dia, n) { cout << i1_dia << " : " << (x)[i1_dia] << '\n'; } cout << endl

#define forifrom(i, s, n) for(ll i = (s); i < (n); ++i)
#define forirto(i, n, e) for(ll i = (n) - 1; i >= (e); --i)
#define fori(i, n) forifrom (i, 0, n)
#define forir(i, n) forirto (i, n, 0)

#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define Add(a, b) a = ((a) + (b)) % MOD
#define Mul(a, b) a = (1LL * (a) * (b)) % MOD

stringstream sss;

// -----------------------------------------------------------------------

const ll maxn = 100010;

ll n, m;
ll W[maxn];

void MAIN() {

    cin >> n >> m;
    fori (i, m) cin >> W[i] >> W[i];
    sort(W, W + m, greater<ll>());
    ll l = 1, r = 3000;
    while (r - l > 1) {
        ll t = (l + r) / 2;
        ll val = t % 2 ? t * (t - 1) / 2 + 1 : t * (t + 1) - 2;
        if (val > n) r = t;
        else l = t;
    }
    smin(l, m);
    ll s = 0;
    fori (i, l) s += W[i];
    cout << s << '\n';

}

// -----------------------------------------------------------------------

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    sss << R"(

    )";
    MAIN();
    return 0;
}


//								be naame khoda

#include <algorithm>
#include <iostream>

using namespace std;

const int maxN = 2*100*1010;

long long dp[maxN], w[maxN];

void filldp(int n)
{
	dp[1] = 1;
	dp[2] = 2;
	for(long long i = 3; i < maxN; i++)
	{
		//dp[i] = dp[i - 1] + i - i % 2;
		dp[i] = i * (i / 2) + i % 2;
	}
}

int main()
{
	long long n, m, q, sw = 0, sk = 0;
	cin >> n >> m;
	for(int i = 0; i < m; i++)
		cin >> q >> w[i];
	sort(w, w + m);
	reverse(w, w + m);
	filldp(maxN);
	while(dp[sk + 1] <= n && sk < m)
	{
		sw += w[sk];
		sk++;
	}
	cout << sw << endl;
	cin >> sk;
}


//#pragma comment(linker, "/STACK:134217728")
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
//#include <ctime>
using namespace std;

typedef pair<int,int> PII;
typedef vector<int> VI;
typedef std::multiset<int>::iterator Iter;

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); --i)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define ALL(a) (a).begin(),(a).end()
#define PB push_back
#define MP make_pair
#define MOD 1000000007
#define INF 1000000007
#define y1 agaga
#define ll long long

int n,m,p;
int q[100000];
long long s[100000];
int w;

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    ios::sync_with_stdio(false);

    cin >> n >> m;

    FOR(i,0,m)
        cin >> w >> q[i];

    sort(q,q+m);
    reverse(q,q+m);

    s[0]=q[0];
    FOR(i,1,m)
        s[i]=s[i-1]+q[i];

    long long l=1;
    long long r=m+1;

    while (r-l>1)
    {
        long long m=(l+r) / 2;

        long long k=m;

        if (k%2==0)
            k=(k)*(k-1) / 2 + k / 2 - 1;
        else
            k=(k)*(k-1) / 2;

        if (k<=n-1) l=m;
        else
            r=m;
    }

    long long sol=s[l-1];

    //cout << l << endl;

    cout << sol << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <map>

using namespace std;

#define SZ(x) (int) (x).size()

typedef long long ll;

const int N = 100010;

struct T
{
    int p, w;
    T (int p, int w) : p(p), w(w) {}
    T() {}
    bool operator < (const T & cur) const
    {
        return w > cur.w;
    }
};

ll f(ll n)
{
    return n % 2 == 0 ? n * n / 2 : n * (n - 1) / 2 + 1;
}

int n, m;
T p[N];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; ++i)
        scanf("%d%d", &p[i].p, &p[i].w);
    sort(p, p + m);
    int len = 0;
    for (int i = 1; ; ++i)
    {
        if (f(i) > n)
            break;
        len = i;
    }
    //cout << len << endl;
    ll ans = 0;
    for (int i = 0; i < min(len, m); ++i)
        ans += p[i].w;
    printf("%I64d", ans);
    return 0;
}


#ifdef NALP_PROJECT
#pragma hdrstop
#else
#define _SECURE_SCL 0
#endif

#define _CRT_SECURE_NO_DEPRECATE
#pragma comment(linker, "/STACK:200000000")

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <utility>
#include <cassert>

#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <bitset>
#include <memory.h>

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

typedef long long int64;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define y1 YYY1
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

#ifdef _WIN32
	#define LLD "%I64d"
#else
	#define LLD "%lld"
#endif

template<typename T> inline T Abs(T x) { return (x >= 0) ? x : -x; }
template<typename T> inline T sqr(T x) { return x * x; }
template<typename T> inline string toStr(T x) { stringstream st; st << x; string s; st >> s; return s; }
template<typename T> inline int bit(T mask, int b) { return (b >= 0 && (mask & (T(1) << b)) != 0) ? 1 : 0; }

inline int nextInt() { int x; if (scanf("%d", &x) != 1) throw; return x; }
inline int64 nextInt64() { int64 x; if (scanf(LLD, &x) != 1) throw; return x; }
inline double nextDouble() { double x; if (scanf("%lf", &x) != 1) throw; return x; }

const int INF = (int)1E9;
const int64 INF64 = (int64)1E18;
const long double EPS = 1E-9;
const long double PI = 3.1415926535897932384626433832795;

const int MAXN = 100100;

int get(int m) {
	if (m % 2 == 1)
		return m * (m - 1) / 2;
	else
		return m * (m - 1) / 2 + m / 2 - 1;
}

bool can(int n, int m) {
	return get(m) < n;
}

int main() {
#ifdef NALP_PROJECT
	freopen("input.txt", "rt", stdin);
//	freopen("output.txt", "wt", stdout);
#else
#endif

	int n = nextInt();
	int m = nextInt();
	vector<int> a;
	forn(i, m) {
		int x = nextInt();
		int y = nextInt();
		a.pb(y);
	}

	sort(rall(a));
	int ans = 0;
	forn(i, a.size()) {
		if (!can(n, i + 1)) break;
		ans += a[i];
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <utility>
#include <functional>

using namespace std;

int main(){
	long m,n,q,w;
	long qw[100000];
	cin >> n >> m;
	long long nc = -1, iC2 = 1;
	if(n == 1)nc = 1;
	else if(n == 2)nc == 2;
	else{
		for(long i = 3; i <= m; i++){
			iC2 += i - 1;
			if(iC2 + ((i + 1) % 2)  > n){
				nc = i - 1;
				break;
			}
		}
		if(nc == -1)nc = m;
	}

	for(int i = 0; i < m; i++){
		cin >> q >> w;
		qw[i] = w;
	}
	sort(qw,qw+m,greater<long>());
	long long res = 0;
	for(int i = 1; i <= nc; i++)
		res += qw[i-1];
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <utility>
#include <functional>

using namespace std;

int main(){
	long m,n,q,w;
	vector<long> qw;
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		cin >> q >> w;
		qw.push_back(w);
	}
	sort(qw.begin(),qw.end(),greater<long>());
	long long nc = -1, iC2 = 1;
	if(n == 1)nc = 1;
	else if(n == 2)nc == 2;
	else{
		for(long i = 3; i <= m; i++){
			iC2 += i - 1;
			if(iC2 + ((i + 1) % 2)  > n){
				nc = i - 1;
				break;
			}
		}
		if(nc == -1)nc = m;
	}
	long long res = 0;
	for(int i = 1; i <= nc; i++)
		res += qw[i-1];
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <utility>
#include <functional>

using namespace std;

int main(){
	long m,n,q,w;
	long qw[100000];
	cin >> n >> m;
	long long nc = -1, iC2 = 1;
	if(n == 1)nc = 1;
	else if(n == 2)nc == 2;
	else{
		for(long i = 3; i <= m; i++){
			iC2 = i * (i - 1) / 2;
			if(iC2 + ((i % 2 ? 0 : (int)(i / 2) - 1)) + 1  > n){
				nc = i - 1;
				break;
			}
		}
		if(nc == -1)nc = m;
	}

	for(int i = 0; i < m; i++){
		cin >> q >> w;
		qw[i] = w;
	}
	sort(qw,qw+m,greater<long>());
	long long res = 0;
	for(int i = 1; i <= nc; i++)
		res += qw[i-1];
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <utility>
#include <functional>

using namespace std;

int main(){
	long m,n,q,w;
	long qw[100000];
	cin >> n >> m;
	long long nc = -1, iC2 = 1;
	if(n == 1)nc = 1;
	else if(n == 2)nc == 2;
	else{
		for(long i = 3; i <= m; i++){
			iC2 += i - 1;
			if(iC2 + ((i % 2 ? 0 : (int)(i / 2) - 1))  > n){
				nc = i - 1;
				break;
			}
		}
		if(nc == -1)nc = m;
	}

	for(int i = 0; i < m; i++){
		cin >> q >> w;
		qw[i] = w;
	}
	sort(qw,qw+m,greater<long>());
	long long res = 0;
	for(int i = 1; i <= nc; i++)
		res += qw[i-1];
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <utility>
#include <functional>

using namespace std;

int main(){
	int m,n,q,w;
	vector<int> qw;
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		cin >> q >> w;
		qw.push_back(w);
	}
	sort(qw.begin(),qw.end(),greater<int>());
	long nc = -1, iC2 = 1;
	if(n == 1)nc = 1;
	else if(n == 2)nc == 2;
	else{
		for(int i = 3; i <= m; i++){
			iC2 += i - 1;
			if(iC2 + ((i + 1) % 2) + 1 > n){
				nc = i - 1;
				break;
			}
		}
		if(nc == -1)nc = m;
	}
	int res = 0;
	for(int i = 1; i <= nc; i++)
		res += qw[i-1];
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <utility>
#include <functional>

using namespace std;

int main(){
	long m,n,q,w;
	vector<int> qw;
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		cin >> q >> w;
		qw.push_back(w);
	}
	sort(qw.begin(),qw.end(),greater<int>());
	long nc = -1, iC2 = 1;
	if(n == 1)nc = 1;
	else if(n == 2)nc == 2;
	else{
		for(int i = 3; i <= m; i++){
			iC2 += i - 1;
			if(iC2 + ((i + 1) % 2) + 1 > n){
				nc = i - 1;
				break;
			}
		}
		if(nc == -1)nc = m;
	}
	long long res = 0;
	for(int i = 1; i <= nc; i++)
		res += qw[i-1];
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <utility>
#include <functional>

using namespace std;

int main(){
	long m,n,q,w;
	vector<int> qw;
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		cin >> q >> w;
		qw.push_back(w);
	}
	sort(qw.begin(),qw.end(),greater<int>());
	long long nc = -1, iC2 = 1;
	if(n == 1)nc = 1;
	else if(n == 2)nc == 2;
	else{
		for(long i = 3; i <= m; i++){
			iC2 += i - 1;
			if(iC2 + ((i + 1) % 2) + 1 > n){
				nc = i - 1;
				break;
			}
		}
		if(nc == -1)nc = m;
	}
	long long res = 0;
	for(int i = 1; i <= nc; i++)
		res += qw[i-1];
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <utility>
#include <functional>

using namespace std;

int main(){
	long m,n,q,w;
	long qw[100000];
	cin >> n >> m;
	long long nc = -1, iC2 = 1;
	if(n == 1)nc = 1;
	else if(n == 2)nc = 2;
	else{
		for(long i = 3; i <= m; i++){
			iC2 = i * (i - 1) / 2;
			if(iC2 + ((i % 2 ? 0 : (int)(i / 2) - 1)) + 1  > n){
				nc = i - 1;
				break;
			}
		}
		if(nc == -1)nc = m;
	}

	for(int i = 0; i < m; i++){
		cin >> q >> w;
		qw[i] = w;
	}
	sort(qw,qw+m,greater<long>());
	long long res = 0;
	for(int i = 1; i <= nc; i++)
		res += qw[i-1];
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <utility>
#include <functional>

using namespace std;

int main(){
	long m,n,q,w;
	vector<int> qw;
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		cin >> q >> w;
		qw.push_back(w);
	}
	sort(qw.begin(),qw.end(),greater<int>());
	long long nc = -1, iC2 = 1;
	if(n == 1)nc = 1;
	else if(n == 2)nc == 2;
	else{
		for(long i = 3; i <= m; i++){
			iC2 += i - 1;
			if(iC2 + ((i + 1) % 2)  > n){
				nc = i - 1;
				break;
			}
		}
		if(nc == -1)nc = m;
	}
	long long res = 0;
	for(int i = 1; i <= nc; i++)
		res += qw[i-1];
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <map>

#define ll long long
#define fs first
#define sc second
#define mp make_pair
#define pb push_back

const ll INF = 1000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

bool cmp(ll a, ll b) {
    if (a > b)
        return true;
    else
        return false;
}

ll f(ll n) {
    if (n % 2 == 1) {
        return n * (n - 1) / 2 + 1;
    }
    else {
        return n * n / 2;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    ll n, m;
    ll a[100005];
    cin >> n >> m;
    for (int i(0); i<m; i++) {
        int t;
        cin >> t >> a[i];
    }
    sort(a, a+m, cmp);
    ll l = 0, r = 1000000000;
    while (l + 1 != r) {
        ll mid = l + (r - l)/2;
        if (f(mid) <= n)
            l = mid;
        else
            r = mid;
    }

    l = min(l, min(n, m));

    ll sum = 0;
    for (int i(0); i<l; i++) {
        sum += a[i];
    }
    cout << sum;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <map>

#define ll long long
#define fs first
#define sc second
#define mp make_pair
#define pb push_back

const ll INF = 1000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

bool cmp(ll a, ll b) {
    if (a > b)
        return true;
    else
        return false;
}

ll f(ll n) {
    if (n % 2 == 0) {
        return n * (n - 1) / 2 + 1;
    }
    else {
        return n * n / 2;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    ll n, m;
    ll a[100005];
    cin >> n >> m;
    for (int i(0); i<m; i++) {
        int t;
        cin >> t >> a[i];
    }
    sort(a, a+m, cmp);
    ll l = 0, r = 1000000000;
    while (l + 1 != r) {
        ll mid = l + (r - l)/2;
        if (f(mid) <= n)
            l = mid;
        else
            r = mid;
    }

    l = min(l, min(n, m));

    ll sum = 0;
    for (int i(0); i<l; i++) {
        sum += a[i];
    }
    cout << sum;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <map>

#define ll long long
#define fs first
#define sc second
#define mp make_pair
#define pb push_back

const ll INF = 1000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

bool cmp(ll a, ll b) {
    if (a > b)
        return true;
    else
        return false;
}

ll f(ll n) {
    if (n % 2 == 0) {
        return n * (n+1) / 2 + 1;
    }
    else {
        return (n+1) * (n+1) / 2;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    ll n, m;
    ll a[100005];
    cin >> n >> m;
    for (int i(0); i<m; i++) {
        int t;
        cin >> t >> a[i];
    }
    sort(a, a+m, cmp);
    ll l = 0, r = 1000000000;
    while (l + 1 != r) {
        ll mid = l + (r - l)/2;
        if (f(mid) <= n)
            l = mid;
        else
            r = mid;
    }

    if (l == 0)
        l = 1;
    l = min(l, min(n, m));

    ll sum = 0;
    for (int i(0); i<l; i++) {
        sum += a[i];
    }
    cout << sum;
}


#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
	//freopen("input.in", "r", stdin);

	long long n, z;
	cin >> n >> z;
	vector<long long> ws(z);
	for (long long i = 0; i < z; i++)
	{
		long long q;
		cin >> q >> ws[i];
	}
	sort(ws.rbegin(), ws.rend());

	long long ans;
	if (n < 3)
	{
		ans = n;
		goto end;
	}

	long long l = 0, r = 10000;
	while (r - l > 1)
	{
		long long m = (r + l) / 2;
		long long val = 2 * m;
		if (val * val / 2 <= n)
			l = m;
		else
			r = m;
	}
	long long a = l;
	l = 0, r = 10000;
	while (r - l > 1)
	{
		long long m = (r + l) / 2;
		long long val = 2 * m + 1;
		if ((val * (val + 1)) / 2 + 1 <= n)
			l = m;
		else
			r = m;
	}
	long long b = l;
	if (b >= a)
	{
		ans = b * 2 + 1;
	}
	else
	{
		ans = a * 2;
	}
end:
	long long sum = 0;
	for (long long i = 0; i < min(ans, z); i++)
		sum += ws[i];

	cout << sum;

	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
	//freopen("input.in", "r", stdin);

	long long n, z;
	cin >> n >> z;
	vector<long long> ws(z);
	for (long long i = 0; i < z; i++)
	{
		long long q;
		cin >> q >> ws[i];
	}
	sort(ws.rbegin(), ws.rend());

	long long ans;
	if (n < 3)
	{
		ans = n;
		goto end;
	}

	long long l = 0, r = 10000;
	while (r - l > 1)
	{
		long long m = (r + l) / 2;
		long long val = 2 * m;
		if (val * val / 2 <= n)
			l = m;
		else
			r = m;
	}
	long long b = l;
	l = 0, r = 10000;
	while (r - l > 1)
	{
		long long m = (r + l) / 2;
		long long val = 2 * m + 1;
		if ((val * (val - 1)) / 2 + 1 <= n)
			l = m;
		else
			r = m;
	}
	long long a = l;
	if (a >= b)
	{
		ans = a * 2 + 1;
	}
	else
	{
		ans = b * 2;
	}
end:
	long long sum = 0;
	for (long long i = 0; i < min(z, ans); i++)
		sum += ws[i];

	cout << sum;

	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
	//freopen("input.in", "r", stdin);

	long long n, z;
	cin >> n >> z;
	vector<long long> ws(z);
	for (long long i = 0; i < z; i++)
	{
		long long q;
		cin >> q >> ws[i];
	}
	sort(ws.rbegin(), ws.rend());

	long long ans;
	if (n < 3)
	{
		ans = n;
		goto end;
	}

	long long l = 0, r = 10000;
	while (r - l > 1)
	{
		long long m = (r + l) / 2;
		long long val = 2 * m;
		if (val * val / 2 <= n)
			l = m;
		else
			r = m;
	}
	long long a = l;
	l = 0, r = 10000;
	while (r - l > 1)
	{
		long long m = (r + l) / 2;
		long long val = 2 * m + 1;
		if ((val * (val + 1)) / 2 + 1 <= n)
			l = m;
		else
			r = m;
	}
	long long b = l;
	if (a >= b)
	{
		ans = a * 2 + 1;
	}
	else
	{
		ans = b * 2;
	}
end:
	long long sum = 0;
	for (long long i = 0; i < min(z, ans); i++)
		sum += ws[i];

	cout << sum;

	return 0;
}


#include <ios>
#include <iostream>
#include <algorithm>
#include <functional>

int a[100005] = {};

long long int cass(long long int x)
{
	if (x & 1) return x*(x-1)/2;
	else return x*(x-1)/2 + (x/2-1);
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	std::cout.tie(NULL);
	int n, m, garbage;
	std::cin >> n >> m;
	for (int i = 0; i < m; i++)
		std::cin >> garbage >> a[i];

	//it's time to play the well-known game,
	//how many elements can we take?!?!
	long long int lo = 0;
	long long int hi = m;
	long long int ans = 0;

	while (lo <= hi)
	{
		long long int mid = (lo+hi)/2;
		if (cass(mid)+1 <= n)
		{
			lo = mid+1;
			ans = mid;
			//ok!
		}
		else
			hi = mid-1;
	}

	std::sort(a, a+m, std::greater<int>());

	long long int sum = 0;
	for (int i = 0; i < ans; i++)
		sum += a[i];
	std::cout << sum << '\n';
}


#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <string>
#include <map>
#define int64 long long
#define rep(i,a,b) for (int i = a; i <= b; i++)
#define repd(i,a,b) for (int i = a; i >= b; i--)
#define mp make_pair
#define N 1000

using namespace std;

int n, m;
int64 q[3000000], w[3000000], dp[3000000];
pair <int64, int64> q1[3000000];

int main()
{
    scanf("%d%d", &n, &m);
    rep(i,1,m) scanf("%I64d%I64d", &q[i], &w[i]);
    rep(i,1,m) q1[i] = mp(w[i], q[i]);
    sort(q1 + 1, q1 + m + 1);
    reverse(q1 + 1, q1 + m + 1);
    dp[1] = 1ll;
    int64 k = 1ll;
    rep(i,2,2000000)
    {
        dp[i] = dp[i - 1] + k;
        k += 1ll;
    }
    k = 0;
    rep(i,1,2000000)
        if (i % 2 == 0)
        {
            dp[i] += k;
            k += 1ll;
        }
    int64 ans = 0, sum = 0;
    rep(i,1,m)
    {
        if (dp[i] > n) break;
        sum += q1[i].first;
        ans = sum;
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

long long a[100002];

int main ()
{
    long long n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++)
        cin >> a[i] >> a[i];
    sort (a, a + m);
    reverse (a, a + m);
    long long ans = 0;
    for (int i = 0; i <= m; i++)
    {
        long long a;
        if (i % 2 == 1)
            a = i * (i - 1) / 2 + 1;
        else
            a = i * (i - 1) / 2 + i / 2;
        if (a <= n)
            ans = a;
        else
            break;
    }
    long long sum = 0;
    for (int i = 0; i < ans; i++)
        sum += a[i];
    cout << sum << endl;
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

long long a[100002];

int main ()
{
    long long n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++)
        cin >> a[i] >> a[i];
    sort (a, a + m);
    reverse (a, a + m);
    long long ans = 0;
    for (int i = 0; i <= m; i++)
    {
        long long a;
        if (i % 2 == 1)
            a = i * (i - 1) / 2 + 1;
        else
            a = i * (i - 1) / 2 + i / 2;
        if (a <= n)
            ans = i;
        else
            break;
    }
    long long sum = 0;
    for (int i = 0; i < ans; i++)
        sum += a[i];
    cout << sum << endl;
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
//#include<cmath>
#include<ctime>

#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<algorithm>
#include<utility>
#include<bitset>
#include<map>
#include<cassert>
//#include<ext/hash_set>
//#include<ext/hash_map>
#define pb push_back
#define ll long long
using namespace std;
#define fi first
#define se second
#define llu unsigned long long

llu get(llu x){
    if(x&1) {
            x/=2;
            return 2* x*x;
    }
    else {
            x--;
            x/=2;
            return 2* x*x +x +1;
    }
}

int main()
{
    //freopen("C:\\a","r",stdin);

    llu n,m;cin>>n>>m;
    vector<llu> in(m);
    for(int i=0;i<m;i++) cin>>in[i],cin>>in[i];
    sort(in.rbegin(),in.rend());
    llu sm=in[0];
    for(int i=2;i<=m;i++){
        if(get(i)>n) break;
        sm+=in[i-1];

    }
    cout<<sm<<endl;

    return 0;
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
//#include<cmath>
#include<ctime>

#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<algorithm>
#include<utility>
#include<bitset>
#include<map>
#include<cassert>
//#include<ext/hash_set>
//#include<ext/hash_map>
#define pb push_back
#define ll long long
using namespace std;
#define fi first
#define se second
#define llu unsigned long long

llu get(llu x){
    if(x==1 ) return 1;
    if(x==2) return 2;
    if(x&1) {
            x++;
            x/=2;
            return 2* x*x;
    }
    else {

            x/=2;
            return 2* x*x +x +1;
    }
}

int main()
{
    //freopen("C:\\a","r",stdin);

    llu n,m;cin>>n>>m;
    vector<llu> in(m);
    for(int i=0;i<m;i++) cin>>in[i],cin>>in[i];
    sort(in.rbegin(),in.rend());
    llu sm=in[0];
    for(int i=2;i<=m;i++){
        if(get(i)>n) break;
        sm+=in[i-1];

    }
    cout<<sm<<endl;

    return 0;
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
//#include<cmath>
#include<ctime>

#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<algorithm>
#include<utility>
#include<bitset>
#include<map>
#include<cassert>
//#include<ext/hash_set>
//#include<ext/hash_map>
#define pb push_back
#define ll long long
using namespace std;
#define fi first
#define se second
#define llu unsigned long long

llu get(llu x){
    if(x==1 ) return 1;
    if(x==2) return 2;
    x--;
    if(x&1) {
            x++;
            x/=2;
            return 2* x*x;
    }
    else {

            x/=2;
            return 2* x*x +x +1;
    }
}

int main()
{
    //freopen("C:\\a","r",stdin);

    llu n,m;cin>>n>>m;
    vector<llu> in(m);
    for(int i=0;i<m;i++) cin>>in[i],cin>>in[i];
    sort(in.rbegin(),in.rend());
    llu sm=in[0];
    for(int i=2;i<=m;i++){
        if(get(i)>n) break;
        sm+=in[i-1];

    }
    cout<<sm<<endl;

    return 0;
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
//#include<cmath>
#include<ctime>

#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<algorithm>
#include<utility>
#include<bitset>
#include<map>
#include<cassert>
//#include<ext/hash_set>
//#include<ext/hash_map>
#define pb push_back
#define ll long long
using namespace std;
#define fi first
#define se second
#define llu unsigned long long

llu get(llu x){

    return (1+x+2*x*x*x)/((x-1)*(x*x-1)*(x*x-1));
}

int main()
{
    //freopen("C:\\a","r",stdin);
    llu n,m;cin>>n>>m;
    vector<llu> in(m);
    for(int i=0;i<m;i++) cin>>in[i],cin>>in[i];
    sort(in.rbegin(),in.rend());
    llu sm=in[0];
    for(int i=2;i<=m;i++){
        if(get(i)>n) break;
        sm+=in[i-1];

    }
    cout<<sm<<endl;

    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<sstream>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;

int arr[100101],rr[100101];

int main(){
    int n,m,i,j,k,a=0,b=0,p;

    cin>>n>>m;
    vector<int> v;

    for(i=0;i<m;i++){
        scanf("%d %d",&a,&k);
        v.push_back(k);
    }

    sort(v.begin(),v.end());
double db;
    db = sqrt((double)n);
    db/=2.0;
    db-=1.0;

    b = int(db);

while((b/2)*b+(b%2) <=n) b++;
b--;
int res=0;
for(i=1;i<=b && i<=m;i++){
    res+=v[m-i];
}
cout<<res<<endl;
return 0;
}


//#pragma comment(linker,"/STACK:1000000000,1000000000")
#define _CRT_SECURE_NO_WARNINGS
#include <cmath>
#include <cassert>
#include <functional>
#include <queue>
#include <time.h>
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
using namespace std;

#define ll long long
#define TASKNAME "invtrans"
#define mp make_pair
#define log rubbish
#define ld long double
#define pii pair<int,int>

const int maxn = 2e6+5;
pii a[maxn];

int main() {
#ifdef EMIL
    freopen("input.txt", "r", stdin);
#else
//	freopen(TASKNAME".in", "r", stdin);
//	freopen(TASKNAME".out", "w", stdout);
#endif
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++)
		cin >> a[i].second >> a[i].first;
	sort(a, a + m);
	int now=1, st=1;
	while (now*2 <= n) {
		now <<= 1;
		st++;
	}
	ll ans=0;
	for (int i=m-1; i>=0; i--) {
		ans += a[i].first;
		st--;
		if (st<=0)
			break;
	}
	cout << ans;
	return 0;
}


//#pragma comment(linker,"/STACK:1000000000,1000000000")
#define _CRT_SECURE_NO_WARNINGS
#include <cmath>
#include <cassert>
#include <functional>
#include <queue>
#include <time.h>
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
using namespace std;

#define ll long long
#define TASKNAME "invtrans"
#define mp make_pair
#define log rubbish
#define ld long double
#define pii pair<int,int>

const int maxn = 2e6+5;
pii a[maxn];

int main() {
#ifdef EMIL
    freopen("input.txt", "r", stdin);
#else
//	freopen(TASKNAME".in", "r", stdin);
//	freopen(TASKNAME".out", "w", stdout);
#endif
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++)
		cin >> a[i].second >> a[i].first;
	sort(a, a + m);
	int st=1;
	for (int i = n; i >= 1; i--) {
		ll c = (i - 1) / 2 + (i - 1) % 2;
		ll cur=c*i+i%2;
		if (cur<=n)
		{
			st=i;
			break;
		}
	}
	ll ans=0;
	for (int i=m-1; i>=0; i--) {
		ans += a[i].first;
		st--;
		if (st<=0)
			break;
	}
	cout << ans;
	return 0;
}


// In the Name Of God

#include <iostream>
#include <fstream>

#include <iomanip>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>

#define inf (int)(~0u/2)
#define ll_inf (int)1 << 62
#define int long long
#define float long double
#define eps (1e-8)
#define for2(i, a, b) for ( int (i) = (a); (i)<(b); (i)++ )
#define mp make_pair
#define f1 first
#define f2 second
#define pb push_back
#define sz(a) (int) a.size( )
#define fillArr(n, a) for2(i, 0, n) cin >> a[i];
#define print(a) cout << #a << endl;

using namespace std;

const int maxn = 3e5+10;
int sum[maxn];

int a[maxn], b[maxn];
vector <int> ans;
vector <int> cost;
void main ( ){
	int n, m;
	cin >> n >> m;
	for2 ( i, 0, m ){
		int a, b;
		cin >> a >> b;
		cost.push_back ( b );
	}
	sort ( cost.rbegin( ), cost.rend( ) );
	for ( int i=m; i>=0; i-- ){
		if ( i * (i-1)/2 <= n-1 && i * (i / 2) <= n ){
			int sum = 0;
			for2 ( j, 0, i ){
				sum += cost[j];
			}
			cout << sum << endl;
			return;
		}
	}
}


#pragma comment(linker, "/STACK:65777216")
#include <iostream>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <string>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <ctime>
#define pb push_back
#define mp make_pair
#define zn first
#define nu second
#define ll long long

using namespace std;

ll n, m;
pair <ll, ll> wq[1000000];
ll ans=0;

bool cmp (pair <ll, ll> x, pair <ll, ll> y)
{
	return x>=y;
}

int z (ll f)
{
	if (f%2==1)
	{
		return f*(f-1)/2+1;
	}
	else
	{
		return f*f/2;
	}
}

int main ()
{
	cin>>n>>m;
	for (int i=0; i<m; i++)
	{
		cin>>wq[i].second>>wq[i].first;
	}
	sort (wq, wq+m, cmp);
	ll l=0, r=100000;
	ll mid=(l+r)/2;
	while (l+1<r)
	{
		mid=(l+r)/2;
		ll g=z(mid);
		if (g>n)
		{
			r=mid;
		}
		else
		{
			l=mid;
		}
	}
	ll ans=0;
	for (int i=0; i<min(l, m); i++)
	{
		ans+=wq[i].first;
	}
	cout<<ans;
}


#include<stdio.h>
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>

using namespace std;

vector< long long int > w;

long long int maxlen(long long int n)
{
	if(n%2==1)
		return n*(n-1)/2+1;
	else
		return n*(n-1)/2+n/2;
}

int main()
{
	long long int n, m;

	scanf("%I64d %I64d", &n, &m);

	long long int money=0;
	long long int qq, ww;
	long long int i;

	for(i=0; i<m; i++)
	{
		scanf("%I64d %I64d", &qq, &ww);
		w.push_back(ww);
	}

	sort(w.begin(), w.end());
	reverse(w.begin(), w.end());

	for(i=0; i<m; i++)
	{
		if(maxlen(i+1)<=n)
			money+=w[i];
		else
			break;
	}

	printf("%I64d\n", money);

	return 0;
}


///#pragma comment(linker,"/STACK:16777216") /*16Mb*/
//#pragma comment(linker,"/STACK:33554432") /*32Mb*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <cmath>
#include <list>
#include <iomanip>
#include <set>
#include <map>

using namespace std;

typedef  long long LL;
typedef  unsigned long long ULL;
typedef  long double LD;
typedef  pair<int,int> PII;
typedef  vector<int> VI;

#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define RFOR(i,a,b) for(int (i)=(a)-1;(i)>=(b);--(i))
#define For(i,a,b) for(int (i)=(a);i<(b);++(i))
#define FoR(i,a,b) for(int (i)=(a-1);(i)>=(b);--(i))
#define MP make_pair
#define MOD 1000000007
#define INF 1000000000
#define PB push_back
#define x0 sdfhrthrth
#define x1 fdhttrlhn
#define y0 kihrbdb
#define y1 ugvrrtgtrg
#define EPS 1e-8
#define X first
#define Y second

int ar [100005];
int n, m;

bool cmp(int a, int b)
{
    return a > b;
}

int main()
{
    //ios_base::sync_with_stdio(0);
    //Read_from_file();
    //freopen("math.in","rt",stdin);freopen("math.out","wt",stdout);
    cin >> n >> m;
    int k;
    For(i,0,m)
        cin >> k >> ar[i];
    sort(ar, ar+m, cmp);

    int l1, l2;
    l1 = (int)((-1 + sqrt(1 + 8*n +0.0))/4);
    l1 = l1*2 + 1;
    l2 = (int)(sqrt(2*n));
    int l = max(l1,l2);
    LL ans = 0;
    For(i,0,min(l,m))
        ans += 1LL*ar[i];
    cout << ans << endl;

    return 0;
}


///#pragma comment(linker,"/STACK:16777216") /*16Mb*/
//#pragma comment(linker,"/STACK:33554432") /*32Mb*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <cmath>
#include <list>
#include <iomanip>
#include <set>
#include <map>

using namespace std;

typedef  long long LL;
typedef  unsigned long long ULL;
typedef  long double LD;
typedef  pair<int,int> PII;
typedef  vector<int> VI;

#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define RFOR(i,a,b) for(int (i)=(a)-1;(i)>=(b);--(i))
#define For(i,a,b) for(int (i)=(a);i<(b);++(i))
#define FoR(i,a,b) for(int (i)=(a-1);(i)>=(b);--(i))
#define MP make_pair
#define MOD 1000000007
#define INF 1000000000
#define PB push_back
#define x0 sdfhrthrth
#define x1 fdhttrlhn
#define y0 kihrbdb
#define y1 ugvrrtgtrg
#define EPS 1e-8
#define X first
#define Y second

int ar [100005];
int n, m;

bool cmp(int a, int b)
{
    return a > b;
}

int main()
{
    //ios_base::sync_with_stdio(0);
    //Read_from_file();
    //freopen("math.in","rt",stdin);freopen("math.out","wt",stdout);
    cin >> n >> m;
    int k;
    For(i,0,m)
        cin >> k >> ar[i];
    sort(ar, ar+m, cmp);

    int l1, l2;
    l1 = (int)((-1 + sqrt(1 + 2*n +0.0))/2);
    l1 = l1*2 + 1;
    l2 = (int)(sqrt(2*n + 0.0));
    int l = max(l1,l2);
    LL ans = 0;
    For(i,0,min(l,m))
        ans += 1LL*ar[i];
    cout << ans << endl;

    return 0;
}


///#pragma comment(linker,"/STACK:16777216") /*16Mb*/
//#pragma comment(linker,"/STACK:33554432") /*32Mb*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <cmath>
#include <list>
#include <iomanip>
#include <set>
#include <map>

using namespace std;

typedef  long long LL;
typedef  unsigned long long ULL;
typedef  long double LD;
typedef  pair<int,int> PII;
typedef  vector<int> VI;

#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define RFOR(i,a,b) for(int (i)=(a)-1;(i)>=(b);--(i))
#define For(i,a,b) for(int (i)=(a);i<(b);++(i))
#define FoR(i,a,b) for(int (i)=(a-1);(i)>=(b);--(i))
#define MP make_pair
#define MOD 1000000007
#define INF 1000000000
#define PB push_back
#define x0 sdfhrthrth
#define x1 fdhttrlhn
#define y0 kihrbdb
#define y1 ugvrrtgtrg
#define EPS 1e-8
#define X first
#define Y second

int ar [100005];
int n, m;

bool cmp(int a, int b)
{
    return a > b;
}

int main()
{
    //ios_base::sync_with_stdio(0);
    //Read_from_file();
    //freopen("math.in","rt",stdin);freopen("math.out","wt",stdout);
    cin >> n >> m;
    int k;
    For(i,0,m)
        cin >> k >> ar[i];
    sort(ar, ar+m, cmp);

    int l1, l2;
    l1 = (int)((-1 + sqrt(1 + 2*n +0.0))/2);
    l1 = l1*2 + 1;
    l2 = (int)(sqrt(2*n));
    int l = max(l1,l2);
    LL ans = 0;
    For(i,0,min(l,m))
        ans += 1LL*ar[i];
    cout << ans << endl;

    return 0;
}


///#pragma comment(linker,"/STACK:16777216") /*16Mb*/
//#pragma comment(linker,"/STACK:33554432") /*32Mb*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <cmath>
#include <list>
#include <iomanip>
#include <set>
#include <map>

using namespace std;

typedef  long long LL;
typedef  unsigned long long ULL;
typedef  long double LD;
typedef  pair<int,int> PII;
typedef  vector<int> VI;

#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define RFOR(i,a,b) for(int (i)=(a)-1;(i)>=(b);--(i))
#define For(i,a,b) for(int (i)=(a);i<(b);++(i))
#define FoR(i,a,b) for(int (i)=(a-1);(i)>=(b);--(i))
#define MP make_pair
#define MOD 1000000007
#define INF 1000000000
#define PB push_back
#define x0 sdfhrthrth
#define x1 fdhttrlhn
#define y0 kihrbdb
#define y1 ugvrrtgtrg
#define EPS 1e-8
#define X first
#define Y second

int ar [100005];
int n, m;

bool cmp(int a, int b)
{
    return a > b;
}

int main()
{
    //ios_base::sync_with_stdio(0);
    //Read_from_file();
    //freopen("math.in","rt",stdin);freopen("math.out","wt",stdout);
    cin >> n >> m;
    int k;
    For(i,0,m)
        cin >> k >> ar[i];
    sort(ar, ar+m, cmp);

    int l1, l2;
    l1 = (int)((-1 + sqrt(1 + 8*n +0.0))/4);
    l1 = l1*2 + 1;
    l2 = (int)(sqrt(2*n + 0.0));
    int l = max(l1,l2);
    LL ans = 0;
    For(i,0,min(l,m))
        ans += 1LL*ar[i];
    cout << ans << endl;

    return 0;
}


///#pragma comment(linker,"/STACK:16777216") /*16Mb*/
//#pragma comment(linker,"/STACK:33554432") /*32Mb*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <cmath>
#include <list>
#include <iomanip>
#include <set>
#include <map>

using namespace std;

typedef  long long LL;
typedef  unsigned long long ULL;
typedef  long double LD;
typedef  pair<int,int> PII;
typedef  vector<int> VI;

#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define RFOR(i,a,b) for(int (i)=(a)-1;(i)>=(b);--(i))
#define For(i,a,b) for(int (i)=(a);i<(b);++(i))
#define FoR(i,a,b) for(int (i)=(a-1);(i)>=(b);--(i))
#define MP make_pair
#define MOD 1000000007
#define INF 1000000000
#define PB push_back
#define x0 sdfhrthrth
#define x1 fdhttrlhn
#define y0 kihrbdb
#define y1 ugvrrtgtrg
#define EPS 1e-8
#define X first
#define Y second

int ar [100005];
int n, m;

bool cmp(int a, int b)
{
    return a > b;
}

int main()
{
    //ios_base::sync_with_stdio(0);
    //Read_from_file();
    //freopen("math.in","rt",stdin);freopen("math.out","wt",stdout);
    cin >> n >> m;
    int k;
    For(i,0,m)
        cin >> k >> ar[i];
    sort(ar, ar+m, cmp);

    int l1 = 100005, l2 = 100004;
    while (1ll*2*(1 + l1/2)*(l1/2) - 1LL*l1/2 + 1LL > 1LL*n)
        l1-= 2;
    while (1LL*l2*l2/2 > n)
        l2 -= 2;

    /*l1 = (int)((-1 + sqrt(1 + 8*n +0.0))/4);
    l1 = l1*2 + 1;
    l2 = (int)(sqrt(2*n));*/
    int l = max(l1,l2);
    LL ans = 0;
    For(i,0,min(l,m))
        ans += 1LL*ar[i];
    cout << ans << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main (){
	//freopen ("input.txt", "r", stdin); freopen ("otput.txt", "w", stdout);
	long long int n, m;
	cin >> n >> m;
	vector <int> a(m);
	int x;
	for (int i = 0; i < m; i++)
		cin >> x >> a[i];
	sort (a.begin(), a.end());
	reverse (a.begin(), a.end());
	long long int min = 2, max = m + 1;
	while (max != min){
		long long int k = min + (max - min) / 2;
		long long int sum = (k % 2 ? k * (k - 1) / 2: k * k / 2);
		if (sum > n) max = k;
			    else min = k + 1;
	}
	long long int ans = 0;
	for (int i = 0; i < max - 1; i++)
		ans += a[i];
	cout << ans;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main (){
	freopen ("input.txt", "r", stdin);
	freopen ("otput.txt", "w", stdout);
	int n, m;
	cin >> n >> m;
	vector <int> a(m);
	int x;
	for (int i = 0; i < m; i++)
		cin >> x >> a[i];
	sort (a.begin(), a.end());
	int min = 2, max = m + 1;
	while (max != min){
		int k = min + (max - min) / 2;
		int sum = (k % 2 ? k * (k - 1) / 2 + 1 : k * k / 2);
		if (sum > n) max = k;
			    else min = k + 1;
	}
	int ans = 0, i = m - 1, j = 0;
	while (i >= 0 && j < max - 1){
		ans += a[i];
		++j;
		--i;
	}
	cout << ans;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main (){
	//freopen ("input.txt", "r", stdin); freopen ("otput.txt", "w", stdout);
	long long int n, m;
	cin >> n >> m;
	vector <int> a(m);
	int x;
	for (int i = 0; i < m; i++)
		cin >> x >> a[i];
	sort (a.begin(), a.end());
	reverse (a.begin(), a.end());
	long long int min = 2, max = m + 1;
	while (max != min){
		int k = min + (max - min) / 2;
		int sum = (k % 2 ? k * (k - 1) / 2: k * k / 2);
		if (sum > n) max = k;
			    else min = k + 1;
	}
	long long int ans = 0;
	for (int i = 0; i < max - 1; i++)
		ans += a[i];
	cout << ans;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main (){
	// freopen ("input.txt", "r", stdin); freopen ("otput.txt", "w", stdout);
	long long int n, m;
	cin >> n >> m;
	vector <int> a(m);
	int x;
	for (int i = 0; i < m; i++)
		cin >> x >> a[i];
	sort (a.begin(), a.end());
	reverse (a.begin(), a.end());
	long long int min = 2, max = m + 1;
	while (max != min){
		int k = min + (max - min) / 2;
		int sum = (k % 2 ? k * (k - 1) / 2 + 1 : k * k / 2);
		if (sum > n) max = k;
			    else min = k + 1;
	}
	long long int ans = 0;
	for (int i = 0; i < max - 1; i++)
		ans += a[i];
	cout << ans;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main (){
	 freopen ("input.txt", "r", stdin); freopen ("otput.txt", "w", stdout);
	long long int n, m;
	cin >> n >> m;
	vector <int> a(m);
	int x;
	for (int i = 0; i < m; i++)
		cin >> x >> a[i];
	sort (a.begin(), a.end());
	reverse (a.begin(), a.end());
	long long int min = 2, max = m + 1;
	while (max != min){
		int k = min + (max - min) / 2;
		int sum = (k % 2 ? k * (k - 1) / 2 + 1 : k * k / 2);
		if (sum > n) max = k;
			    else min = k + 1;
	}
	long long int ans = 0;
	for (int i = 0; i < max - 1; i++)
		ans += a[i];
	cout << ans;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main (){
	//freopen ("input.txt", "r", stdin); freopen ("otput.txt", "w", stdout);
	long long int n, m;
	cin >> n >> m;
	vector <int> a(m);
	int x;
	for (int i = 0; i < m; i++)
		cin >> x >> a[i];
	sort (a.begin(), a.end());
	reverse (a.begin(), a.end());
	long long int min = 2, max = m + 1;
	while (max != min){
		long long int k = min + (max - min) / 2;
		long long int sum = (k % 2 ? k * (k - 1) / 2 + 1: k * k / 2);
		if (sum > n) max = k;
			    else min = k + 1;
	}
	long long int ans = 0;
	for (int i = 0; i < max - 1; i++)
		ans += a[i];
	cout << ans;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main (){
	// freopen ("input.txt", "r", stdin); freopen ("otput.txt", "w", stdout);
	long long int n, m;
	cin >> n >> m;
	vector <int> a(m);
	int x;
	for (int i = 0; i < m; i++)
		cin >> x >> a[i];
	sort (a.begin(), a.end());
	reverse (a.begin(), a.end());
	long long int min = 2, max = m + 1;
	while (max != min){
		int k = min + (max - min) / 2;
		int sum = (k % 2 ? k * (k - 1) / 2 + 1 : k * k / 2);
		if (sum > n) max = k;
			    else min = k + 1;
	}
	long long int ans;
	for (int i = 0; i < max - 1; i++)
		ans += a[i];
	cout << ans;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main (){
	// freopen ("input.txt", "r", stdin); freopen ("otput.txt", "w", stdout);
	int n, m;
	cin >> n >> m;
	vector <int> a(m);
	int x;
	for (int i = 0; i < m; i++)
		cin >> x >> a[i];
	sort (a.begin(), a.end());
	int min = 2, max = m + 1;
	while (max != min){
		int k = min + (max - min) / 2;
		int sum = (k % 2 ? k * (k - 1) / 2 + 1 : k * k / 2);
		if (sum > n) max = k;
			    else min = k + 1;
	}
	int ans = 0, i = m - 1, j = 0;
	while (i >= 0 && j < max - 1){
		ans += a[i];
		++j;
		--i;
	}
	cout << ans;
}


#pragma warning(disable:4996)
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <set>
#include <queue>
#include <cstring>
#include <string>
#include <ctime>
#include <stack>
#include <cstdlib>
#define INF 0x7fffffff
#define eps 1e-5
using namespace std;
typedef long long int64;
int w[2000030];
int n, m;
bool cmp(int a, int b)
{
	return a > b;
}
int main()
{
	int q;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; i++)
		scanf("%d%d", &q, &w[i]);
	sort(w + 1, w + 1 + m);
	int64 sum = 0;
	int64 temp;
	for (int i = 1; i <= m; i++)
	{
		if (i % 2 == 0)
			temp = (i - 1) / 2 * i + i;
		else
			temp = (i - 1) / 2 * i + 1;
		if (temp <= n)
			sum += w[m - i + 1];
		else break;
	}
	printf("%I64d\n", sum);
	//system("pause");
	return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <assert.h>
#include <queue>
#include <cstring>
#include <numeric>
#include <hash_map>

using namespace std;

typedef long long ll;
#define mp make_pair

int n, m;

int l2(int t)
{
	int cur = 1;
	int step = 1;
	while (cur * 2 <= t)
		cur *= 2, step++;
	return step;
}

struct T
{
	int q, w;
};

bool cmp(T a, T b)
{
	if (a.w != b.w)
		return a.w < b.w;
	return a.q < b.q;
}

const int N = 1e5 + 100;

T a[N];

ll f(ll t)
{
	if (t & 1)
		return t * (t - 1) / 2;
	return t * (t - 1) / 2 + t / 2;
}

int main()
{

	scanf("%d %d", &n, &m);

	for (int i = 0; i < m; i++)
		scanf("%d %d", &a[i].q, &a[i].w);
	sort(a, a + m, cmp);
	reverse(a, a + m);
	ll ans = 0;
	int r = 0;
	for (int i = 1; 1; i++)
	{
		if (f(i + 1) > n)
		{
			r = i;
			break;
		}
	}
	for (int i = 0; i < min(r, m); i++)
		ans += a[i].w;
	printf("%I64d", ans);

	return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <assert.h>
#include <queue>
#include <cstring>
#include <numeric>
#include <hash_map>

using namespace std;

typedef long long ll;
#define mp make_pair

int n, m;

int l2(int t)
{
	int cur = 1;
	int step = 1;
	while (cur * 2 <= t)
		cur *= 2, step++;
	return step;
}

struct T
{
	int q, w;
};

bool cmp(T a, T b)
{
	if (a.w != b.w)
		return a.w < b.w;
	return a.q < b.q;
}

const int N = 1e5 + 100;

T a[N];

int main()
{

	scanf("%d %d", &n, &m);

	for (int i = 0; i < m; i++)
		scanf("%d %d", &a[i].q, &a[i].w);
	sort(a, a + m, cmp);
	reverse(a, a + m);
	ll ans = 0;
	int r = l2(n);
	for (int i = 0; i < min(r, m); i++)
		ans += a[i].w;
	printf("%I64d", ans);

	return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <assert.h>
#include <queue>
#include <cstring>
#include <numeric>
#include <hash_map>

using namespace std;

typedef unsigned long long ll;
#define mp make_pair

ll n, m;

struct T
{
	ll q, w;
};

bool cmp(T a, T b)
{
	if (a.w != b.w)
		return a.w < b.w;
	return a.q < b.q;
}

const ll N = 2e6 + 100;

T a[N];

ll f(ll t)
{
	if (t & 1)
		return t * (t - 1) / 2;
	return t * (t - 1) / 2 + t / 2;
}

int main()
{

	cin >> n >> m;

	for (ll i = 0; i < m; i++)
		cin >> a[i].q >> a[i].w;
	sort(a, a + m, cmp);
	reverse(a, a + m);
	ll ans = 0;
	ll r = 0;
	for (ll i = 0; 1; i++)
	{
		if (f(i + 1) > n)
		{
			r = i;
			break;
		}
	}
	for (ll i = 0; i < min(r, m); i++)
		ans += a[i].w;
	cout << ans;

	return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <assert.h>
#include <queue>
#include <cstring>
#include <numeric>
#include <hash_map>

using namespace std;

typedef long long ll;
#define mp make_pair

ll n, m;

ll l2(ll t)
{
	ll cur = 1;
	ll step = 1;
	while (cur * 2 <= t)
		cur *= 2, step++;
	return step;
}

struct T
{
	ll q, w;
};

bool cmp(T a, T b)
{
	if (a.w != b.w)
		return a.w < b.w;
	return a.q < b.q;
}

const ll N = 3e5 + 100;

T a[N];

ll f(ll t)
{
	if (t & 1)
		return t * (t - 1) / 2;
	return t * (t - 1) / 2 + t / 2;
}

int main()
{
	scanf("%I64d %I64d", &n, &m);

	for (ll i = 0; i < m; i++)
		scanf("%I64d %I64d", &a[i].q, &a[i].w);
	sort(a, a + m, cmp);
	reverse(a, a + m);
	ll ans = 0;
	ll r = 0;
	for (ll i = 1; 1; i++)
	{
		if (f(i + 1) > n)
		{
			r = i;
			break;
		}
	}
	for (ll i = 0; i < min(r, m); i++)
		ans += a[i].w;
	printf("%I64d", ans);

	return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <assert.h>
#include <queue>
#include <cstring>
#include <numeric>
#include <hash_map>

using namespace std;

typedef long long ll;
#define mp make_pair

ll n, m;

ll l2(ll t)
{
	ll cur = 1;
	ll step = 1;
	while (cur * 2 <= t)
		cur *= 2, step++;
	return step;
}

struct T
{
	ll q, w;
};

bool cmp(T a, T b)
{
	if (a.w != b.w)
		return a.w < b.w;
	return a.q < b.q;
}

const ll N = 1e5 + 100;

T a[N];

ll f(ll t)
{
	if (t & 1)
		return t * (t - 1) / 2;
	return t * (t - 1) / 2 + t / 2;
}

int main()
{

	scanf("%I64d %I64d", &n, &m);

	for (ll i = 0; i < m; i++)
		scanf("%I64d %I64d", &a[i].q, &a[i].w);
	sort(a, a + m, cmp);
	reverse(a, a + m);
	ll ans = 0;
	ll r = 0;
	for (ll i = 1; 1; i++)
	{
		if (f(i + 1) > n)
		{
			r = i;
			break;
		}
	}
	for (ll i = 0; i < min(r, m); i++)
		ans += a[i].w;
	printf("%I64d", ans);

	return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <assert.h>
#include <queue>
#include <cstring>
#include <numeric>
#include <hash_map>

using namespace std;

typedef unsigned long long ll;
#define mp make_pair

ll n, m;

struct T
{
	ll q, w;
};

bool cmp(T a, T b)
{
	if (a.w != b.w)
		return a.w < b.w;
	return a.q < b.q;
}

const ll N = 2e6 + 100;

T a[N];

ll f(ll t)
{
	if (t & 1)
		return t * (t - 1) / 2;
	return t * (t - 1) / 2 + t / 2 - 1;
}

int main()
{

	cin >> n >> m;

	for (ll i = 0; i < m; i++)
		cin >> a[i].q >> a[i].w;
	sort(a, a + m, cmp);
	reverse(a, a + m);
	ll ans = 0;
	ll r = 0;
	for (ll i = 1; 1; i++)
	{
		if (f(i + 1) > n)
		{
			r = i;
			break;
		}
	}
	for (ll i = 0; i < min(r, m); i++)
		ans += a[i].w;
	cout << ans;

	return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <assert.h>
#include <queue>
#include <cstring>
#include <numeric>
#include <hash_map>

using namespace std;

typedef unsigned long long ll;
#define mp make_pair

ll n, m;

ll l2(ll t)
{
	ll cur = 1;
	ll step = 1;
	while (cur * 2 <= t)
		cur *= 2, step++;
	return step;
}

struct T
{
	ll q, w;
};

bool cmp(T a, T b)
{
	if (a.w != b.w)
		return a.w < b.w;
	return a.q < b.q;
}

const ll N = 2e6 + 100;

T a[N];

ll f(ll t)
{
	if (t & 1)
		return t * (t - 1) / 2;
	return t * (t - 1) / 2 + t / 2;
}

int main()
{

	cin >> n >> m;

	for (ll i = 0; i < m; i++)
		cin >> a[i].q >> a[i].w;
	sort(a, a + m, cmp);
	reverse(a, a + m);
	ll ans = 0;
	ll r = 0;
	for (ll i = 1; 1; i++)
	{
		if (f(i + 1) > n)
		{
			r = i;
			break;
		}
	}
	for (ll i = 0; i < min(r, m); i++)
		ans += a[i].w;
	cout << ans;

	return 0;
}