#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
//#include <cstdio>
#include <stdio.h>
class Cooperative {
public:
int q;
int r;
int q_lowlimit;
Cooperative(int q, int r) : q(q), r(r) {}
~Cooperative() {}
friend bool operator<(const Cooperative &a, const Cooperative &b) { return !(a.q < b.q); }
friend std::ostream &operator<<(std::ostream &s, Cooperative &c) {
return s << "(" << c.q << "," << c.r << "," << c.q_lowlimit << ")"; }
};
void setq_lowlimit(std::vector<Cooperative>&c, int m) {
int lowlimit = m;
int i;
for (i = c.size() - 1; i >= 0; --i) {
if (lowlimit <= 0)
break;
c[i].q_lowlimit = lowlimit;
lowlimit -= c[i].q;
}
if (lowlimit <= 0)
for (;i >= 0; --i)
c[i].q_lowlimit = 0;
}
void read_data(int &m, int &n, std::vector<Cooperative>&c) {
#define ALPHA 1
#if 0
std::cin >> m;
std::cin >> n;
c.reserve(n * ALPHA);
for (int i = 0; i < n; i++) {
int q, r;
std::cin >> q, std::cin >> r;
c.push_back(Cooperative(q, r));
#else
scanf("%d", &m);
scanf("%d", &n);
c.reserve(n * ALPHA);
for (int i = 0; i < n; i++) {
int q, r;
scanf("%d %d\n", &q, &r);
c.push_back(Cooperative(q, r));
#endif
}
}
#include <cstdlib>
static int const N = 25;
static int const MAX1 = 100;
static int const MAX2 = 10000;
static int const seed = 31415926;
void make_data(int &m, int &n, std::vector<Cooperative>&c) {
srand(seed);
n = N;
m = 0;
c.reserve(n * ALPHA);
for (int i = 0; i < n; i++) {
int q, r;
q = rand() % MAX1 + 1;
r = rand() % MAX2 + 1;
m += q;
c.push_back(Cooperative(q, r));
}
m /= 4;
}
void dump(int m, int n, std::vector<Cooperative>&c) {
std::cout << "m = " << m << std::endl;
std::cout << "n = " << n << std::endl;
for (size_t i = 0; i < c.size(); i++)
std::cout << c[i] << ", ";
std::cout << std::endl;
}
/*-----------------------------------------------------------------*/
class Phase {
public:
int mTotal;
int rTotal;
int last_pos;
Phase(int idx, int q, int r) : mTotal(q), rTotal(r), last_pos(idx) {}
Phase(const Phase *org) {
this->mTotal = org->mTotal;
this->rTotal = org->rTotal;
}
void addCooperative(int idx, int q, int r) {
this->mTotal += q;
this->rTotal += r;
this->last_pos = idx;
}
};
class ComparePriorityA {
public:
bool operator()(const Cooperative &a, const Cooperative &b) {
return !(a.q < b.q);
}
};
class ComparePriorityB {
public:
bool operator()(Phase *a, Phase *b) {
return !(a->mTotal < b->mTotal);
}
};
void searchPreparation(std::priority_queue<Phase *, std::vector<Phase *>, ComparePriorityB>&q, std::vector<Cooperative>&c) {
for (int i = 0; i < c.size(); i++)
q.push(new Phase(i, c[i].q, c[i].r));
}
int searchLoop(std::priority_queue<Phase *, std::vector<Phase *>, ComparePriorityB>&q, std::vector<Cooperative>&c, int m) {
int min_r = 0;
int t;
Phase *nowchecking;
for (int i = 0; i < c.size(); i++) min_r += c[i].r;
while (!q.empty()) {
nowchecking = q.top();
q.pop();
// std::cout << nowchecking->mTotal << std::endl;
if (nowchecking->mTotal >= m) {
if ((t = nowchecking->rTotal) < min_r) {
min_r = t;
}
delete nowchecking;
} else { /* making child-node and add those to queue and deleted himself */
for (int i = nowchecking->last_pos + 1; i < c.size(); i++) {
if (nowchecking->rTotal + c[i].r >= min_r)
continue;
if (nowchecking->mTotal + c[i].q < c[i].q_lowlimit)
continue;
Phase *Child = new Phase(nowchecking);
Child->addCooperative(i, c[i].q, c[i].r);
q.push(Child);
}
delete nowchecking;
}
} /* !q.empty()? */
return min_r;
}
int searchBestSolution(int m, std::vector<Cooperative>&c) {
std::priority_queue<Phase *, std::vector<Phase *>, ComparePriorityB> q;
searchPreparation(/*ref*/q, /*ref*/c);
return searchLoop(/*ref*/q, /*ref*/c, m);
}
/*-----------------------------------------------------------------*/
#include <cassert>
int addCombinationAll(std::vector<int> &c) {
int n;
for (n = c.size() - 1; n >= 0; --n) {
if (!c[n]) {
c[n] = 1;
return 0;
}
c[n] = 0;
}
assert(n < 0);
return 1;
}
int reference(int m, std::vector<Cooperative>&c) {
std::vector<int> combinationAll(c.size(), 0);
int min_r;
for (int i = 0; i < c.size(); i++) min_r += c[i].r;
while (!addCombinationAll(combinationAll)) {
int rTotal = 0;
int mTotal = 0;
for (int i = 0; i < c.size(); i++) {
if (combinationAll[i]) {
rTotal += c[i].r;
mTotal += c[i].q;
}
}
if (mTotal >= m && rTotal < min_r)
min_r = rTotal;
}
return min_r;
}
/*-----------------------------------------------------------------*/
#include <ctime>
int main() {
int m, n;
std::vector<Cooperative> c;
read_data(/*ref*/m, /*ref*/n, /*ref*/c);
// make_data(m, n, c);
sort(c.begin(), c.end(), ComparePriorityA());
setq_lowlimit(/*ref*/c, m);
// dump(m, n, /*ref*/c);
// std::cout << reference(m, /*ref*/c) << std::endl;
// time_t t = time(0);
std::cout << searchBestSolution(m, /*ref*/c) << std::endl;
// std::cout << time(0) - t << std::endl;
return 0;
}
/* end */
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxhbGdvcml0aG0+Ci8vI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPHN0ZGlvLmg+CgpjbGFzcyBDb29wZXJhdGl2ZSB7CnB1YmxpYzoKICBpbnQgcTsKICBpbnQgcjsKICBpbnQgcV9sb3dsaW1pdDsKICBDb29wZXJhdGl2ZShpbnQgcSwgaW50IHIpIDogcShxKSwgcihyKSB7fQogIH5Db29wZXJhdGl2ZSgpIHt9CiAgZnJpZW5kIGJvb2wgb3BlcmF0b3I8KGNvbnN0IENvb3BlcmF0aXZlICZhLCBjb25zdCBDb29wZXJhdGl2ZSAmYikgeyByZXR1cm4gIShhLnEgPCBiLnEpOyB9CiAgZnJpZW5kIHN0ZDo6b3N0cmVhbSAmb3BlcmF0b3I8PChzdGQ6Om9zdHJlYW0gJnMsIENvb3BlcmF0aXZlICZjKSB7CiAgICByZXR1cm4gcyA8PCAiKCIgPDwgYy5xIDw8ICIsIiA8PCBjLnIgPDwgIiwiI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