ソースコード
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
int n, p[1000], s, yan;
int b = 50000000000;
int c = 0;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> p[i];
}
cin >> s;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (p[i] > p[j]) {
yan = p[i];
p[i] = p[j];
p[j] = yan;
}
}
}
if (s == 0) {
cout << p[0] << endl;
return 0;
}
for (int i = n - 1; i >= 0;i=i-1) {
if (p[i] >= s) {
b = p[i];
}
else {
c += p[i];
if (c >= s) {
if (b >= c&&c >= s) {
cout << c << endl;
return 0;
}
else {
if (b >= c&&c <= s) {
cout << b << endl;
return 0;
}
else {
if (c >= b&&b >= s) {
cout << b << endl;
return 0;
}
else {
cout << c << endl;
return 0;
}
}
}