#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
int a[1005];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
// 对数组进行排序
qsort(a, n, sizeof(int), compare);
long long total = 0;
int left = 0, right = n - 1;
// 使用双指针方法进行计算
while (right - left > 2) {
long long s1 = 2LL * a[left] + a[right] + a[right - 1];
long long s2 = a[left] + 2LL * a[left + 1] + a[right];
total += (s1 < s2) ? s1 : s2;
right -= 2;
}
// 处理剩下的3个或更少的元素
if (right - left == 2) {
total += a[left] + a[left + 1] + a[left + 2];
} else if (right - left == 1) {
total += a[left + 1];
} else if (right - left == 0) {
total += a[left];
}
// 输出结果
printf("%lld\n", total); // 使用%lld适应Linux环境
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
int a[1005];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
qsort(a, n, sizeof(int), compare);
long long total = 0;
int m = n;
while (m > 3) {
long long s1 = 2LL * a[0] + a[m-1] + a[m-2];
long long s2 = a[0] + 2LL * a[1] + a[m-1];
total += (s1 < s2) ? s1 : s2;
m -= 2;
}
if (m == 3) {
total += a[0] + a[1] + a[2];
} else if (m == 2) {
total += a[1];
} else if (m == 1) {
total += a[0];
}
printf("%lld\n", total);
}
return 0;
}
为何在浙理工的oj系统里过不了,如何解决