#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
struct node {
int p;
int m;
} a[N];
int v, n;
bool cmp(node A, node B) {
return A.p > B.p;
}
int main() {
while (~scanf("%d", &v)) {
if (v == 0)
break;
scanf("%d", &n);
for (int i = 1; i <= n; i ++ )
scanf("%d%d", &a[i].p, &a[i].m);
sort(a + 1, a + 1 + n, cmp);
int sum = 0;
for (int i = 1; i <= n; i ++ ) {
if (v >= a[i].m) {
v -= a[i].m;
sum += a[i].p * a[i].m;
} else {
sum += a[i].p * v;
break;
}
}
printf("%d\n", sum);
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1010;
struct hw {
int de;
int sc;
} work[N];
int n;
bool st[N];
bool cmp(hw a, hw b) {
if (a.sc != b.sc)
return a.sc > b.sc;
return a.de < b.de;
}
int main() {
int T;
scanf("%d", &T);
while (T -- ) {
scanf("%d", &n);
memset(st, false, sizeof st);
for (int i = 1; i <= n; i ++ )
scanf("%d", &work[i].de);
for (int i = 1; i <= n; i ++ )
scanf("%d", &work[i].sc);
sort(work + 1, work + n + 1, cmp);
int ans = 0;
int j = 0;
for (int i = 1; i <= n; i ++ ) {
for (j = work[i].de; j > 0; j -- ) {
if (!st[j]) {
st[j] = true;
break;
}
}
if (j == 0)
ans += work[i].sc;
}
printf("%d\n", ans);
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 1010;
int n, m;
int main() {
int T;
scanf("%d", &T);
while (T -- ) {
scanf("%d%d", &n, &m);
int ans = 0;
for (int pos = min(30, m); pos >= 0; pos -- ) {
int t;
t = pow(2, pos);
if (t > n)
continue;
ans += n / t;
n %= t;
}
printf("%d\n", ans);
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 410;
int n;
int st[N];
int main() {
int T;
scanf("%d", &T);
while (T -- ) {
memset(st, 0, sizeof st);
scanf("%d", &n);
while (n -- ) {
int l, r;
scanf("%d%d", &l, &r);
if (l > r)
swap(l, r);
if (l % 2 == 0)
l -- ;
if (r % 2 == 1)
r ++ ;
for (int i = l; i <= r; i ++ )
st[i] += 10;
}
printf("%d\n", *max_element(st, st + N));
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1010;
int a[N];
int n, m, casei, x;
int main() {
while (~scanf("%d %d", &m, &n)) {
if (n == 0 && m == 0)
break;
memset(a, 0, sizeof a);
for (int i = 1; i <= n; i ++ ) {
scanf("%d", &x);
a[x] = 1;
}
int res = 0, big = 0;
for (int i = n * m; i > 0; i -- ) {
if (a[i]) {
if (!big)
res ++ ;
else
big -- ;
} else
big ++ ;
}
printf("Case %d: %d\n", ++ casei, res);
}
return 0;
}