#pragma GCC optimize ("O3") #pragma GCC target ("avx") #include "bits/stdc++.h" // define macro "/D__MAI" using namespace std; typedef long long int ll; #define xprintf(fmt,...) fprintf(stderr,fmt,__VA_ARGS__) #define debugv(v) {printf("L%d %s > ",__LINE__,#v);for(auto e:v){cout< ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[x][y]<<" ";}cout<\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[y][x]<<" ";}cout< ostream& operator <<(ostream &o, const pair p) { o << "(" << p.first << ":" << p.second << ")"; return o; } mt19937 mt(8901016); inline int rand_int(int l, int h) { return uniform_int_distribution<>(l, h)(mt); } #ifdef __MAI #define getchar_unlocked getchar #define putchar_unlocked putchar #endif #ifdef __VSCC #define getchar_unlocked _getchar_nolock #define putchar_unlocked _putchar_nolock #endif namespace { #define isvisiblechar(c) (0x21<=(c)&&(c)<=0x7E) class MaiScanner { public: template void input_integer(T& var) { var = 0; T sign = 1; int cc = getchar_unlocked(); for (; cc<'0' || '9'>(int& var) { input_integer(var); return *this; } inline MaiScanner& operator>>(long long& var) { input_integer(var); return *this; } inline MaiScanner& operator>>(string& var) { int cc = getchar_unlocked(); for (; !isvisiblechar(cc); cc = getchar_unlocked()); for (; isvisiblechar(cc); cc = getchar_unlocked()) var.push_back(cc); } template void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; } }; } MaiScanner scanner; int n, len; int result = -1; int dd[20]; //map, int> memo; int dfs(vector used, int life, int max_life, int m = 0) { if (m == n) return life; // auto memoit = memo.find(used); // if (memoit != memo.end() && memoit->second > life) return 0; // memo[used] = life; int best = 0; repeat(i, n) { if (used[i]) continue; vector new_used = used; new_used[i] = true; if (dd[i] < 0) { // わるいスライム int new_life = life + dd[i]; if (new_life <= 0) continue; best = max(best, dfs(new_used, new_life, max_life + 100, m + 1)); } else if (life < max_life) { // ホイミン int new_life = min(max_life, life + dd[i]); best = max(best, dfs(new_used, new_life, max_life, m + 1)); } } return best; } int main() { scanner >> n; repeat(i, n) { scanner >> dd[i]; } sort(dd, dd + n); cout << dfs(vector(n), 100, 100) << endl; return 0; }