#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int p[N];
int R, n;
int main() {
while (~scanf("%d%d", &R, &n)) {
if (R == -1 && n == -1)
break;
for (int i = 1; i <= n; i ++ )
scanf("%d", &p[i]);
sort(p + 1, p + 1 + n);
int cnt = 1;
int ans = 0;
while (cnt <= n) {
ans ++ ;
int l = p[cnt];
cnt ++ ;
while (cnt <= n && p[cnt] <= l + R)
cnt ++ ;
int flag = p[cnt - 1];
while (cnt <= n && p[cnt] <= flag + R)
cnt ++ ;
}
printf("%d\n", ans);
}
return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
int main() {
int n;
scanf("%d", &n);
priority_queue<int, vector<int>, greater<int>> heap;
for (int i = 0; i < n; i ++ ) {
int x;
scanf("%d", &x);
heap.push(x);
}
LL ans = 0;
while (heap.size() > 1) {
int a = heap.top();
heap.pop();
int b = heap.top();
heap.pop();
ans += a + b;
heap.push(a + b);
}
printf("%lld\n", ans);
return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5010;
struct node {
int l, w;
} s[N];
bool st[N];
int n;
bool cmp(node A, node B) {
return A.l < B.l || (A.l == B.l && A.w < B.w);
}
int main() {
int T;
scanf("%d", &T);
while (T -- ) {
memset(st, false, sizeof st);
scanf("%d", &n);
for (int i = 1; i <= n; i ++ )
scanf("%d%d", &s[i].l, &s[i].w);
sort(s + 1, s + 1 + n, cmp);
int ans = 0, cnt = 1;
while (cnt <= n) {
ans ++ ;
cnt ++ ;
int j = 1;
while (st[j])
j ++ ;
st[j] = true;
for (int k = j + 1; k <= n; k ++ )
if (!st[k] && s[k].w >= s[j].w) {
cnt ++ ;
st[k] = true;
j = k;
}
}
printf("%d\n", ans);
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 30;
struct node {
int v, w;
} a[N];
int n, m;
int num, ans, cnt;
bool cmp(node A, node B) {
return A.v > B.v;
}
int main() {
while (~scanf("%d%d", &n, &m)) {
ans = cnt = 0;
for (int i = 0; i < n; i ++ ) {
scanf("%d%d", &a[i].v, &a[i].w);
//排除直接可以支付的
if (a[i].v >= m)
ans += a[i].w, i --, n -- ;
else
cnt += a[i].w;
}
sort(a, a + n, cmp);
while (cnt) {
num = m;
//从大到小给钱
for (int i = 0; i < n; i ++ ) {
if (!a[i].w)
continue;
if (num > 0) {
int tmp = min(num / a[i].v, a[i].w);
a[i].w -= tmp;
cnt -= tmp;
num -= tmp * a[i].v;
}
if (num <= 0)
break;
}
//从小到大给钱
if (num > 0) {
for (int i = n - 1; i >= 0; i -- ) {
if (a[i].w) {
while (num > 0 && a[i].w)
num -= a[i].v, a[i].w--, cnt--;
if (num <= 0)
break;
}
}
}
if (num <= 0)
ans++;
}
printf("%d\n", ans);
}
return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int n;
int t[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i ++ )
scanf("%d", &t[i]);
sort(t + 1, t + 1 + n);
int ans = 0;
int m = n;
while (m) {
if (m == 1) {
ans += t[m];
m = 0;
} else if (m == 2) {
ans += t[m];
m = 0;
} else if (m == 3) {
ans += t[m] + t[1] + t[2];
m = 0;
} else {
if ((t[m] + t[1] + t[m - 1] + t[1]) > (t[2] + t[1] + t[m] + t[2]))
ans += t[2] + t[1] + t[m] + t[2];
else
ans += t[m] + t[1] + t[m - 1] + t[1];
m -= 2;
}
}
printf("%d\n", ans);
while (n) {
if (n == 1) {
printf("%d\n", t[n]);
n = 0;
} else if (n == 2) {
printf("%d %d\n", t[1], t[n]);
n = 0;
} else if (n == 3) {
printf("%d %d\n%d\n%d %d\n", t[1], t[2], t[1], t[1], t[n]);
n = 0;
} else {
if ((t[n] + t[1] + t[n - 1] + t[1]) > (t[2] + t[1] + t[n] + t[2]))
printf("%d %d\n%d\n%d %d\n%d\n", t[1], t[2], t[1], t[n - 1], t[n], t[2]);
else
printf("%d %d\n%d\n%d %d\n%d\n", t[1], t[n], t[1], t[1], t[n - 1], t[1]);
n -= 2;
}
}
return 0;
}