#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair P; typedef tuple T; typedef vector vec; #define For(i,a,b) for(ll (i) = (a);i < (b);(i)++) #define rep(i,n) For(i,0,n) #define rFor(i,a,b) for(ll (i) = (a-1);i >= (b);(i)--) #define rrep(i,n) rFor(i,n,0) #define clr(a) memset((a), 0 ,sizeof(a)) #define mclr(a) memset((a), -1 ,sizeof(a)) #define all(a) (a).begin(),(a).end() #define sz(a) (sizeof(a)) #define toint toInt #define tostr toString const int dx[8] = { 1, 0, -1, 0, 1, 1, -1, -1 }, dy[8] = { 0, -1, 0, 1, -1, 1, -1, 1 }; const int mod = 1000000007; const int INF = 1e9 + 2; template inline T sqr(T x) { return x*x; } inline bool cheak(int x, int y, int xMax, int yMax){ return x >= 0 && y >= 0 && xMax > x && yMax > y; } inline ll toInt(string s) { ll v; istringstream sin(s); sin >> v; return v; } template inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } int dp[1 << 15]; int main(){ int n; cin >> n; vector m(n); rep(i, n){ cin >> m[i]; } rep(i, 1 << 15)dp[i] = INF; dp[0] = 0; rep(bit, 1 << n){ int v = 0; rep(i, n){ if ((1 << i) & bit){ v += m[i]; } } rep(i, n){ if (!((1 << i) & bit)){ dp[bit | (1 << i)] = min(dp[bit | (1 << i)], dp[bit] + max(0, m[i] - (v % 1000))); } } } cout << dp[(1 << n) - 1] << endl; return 0; }