#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <cmath>
# include <algorithm>
# include <stdio.h>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <random>
# include <deque>
# include <functional>
# include <iomanip>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
# include <immintrin.h>
# include <cassert>
# include <array>

using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_set>
# include <unordered_map>
# else
# include <tr1/unordered_map>
# include <tr1/unordered_set>
using namespace tr1;
# endif
# else
# include <unordered_map>
# include <unordered_set>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cerr<<#a<<"=["; printarray(a,n); cerr<<"]\n"
#define DAR(a,n,s) cerr<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cerr<<"]\n"

#ifdef _MSC_VER
#define ALIGN(x) __declspec(align(x))
#define PREFETCH(ptr, rw, level) ((void)0)
#else
#define ALIGN(x) __attribute__((aligned(x)))
#define PREFETCH(ptr, rw, level) __builtin_prefetch(ptr, rw, level)
#endif

#ifdef LOCAL
#define CURTIME() cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl
#else
#define CURTIME() ;
#endif
double __begin;
#define DTIME(ccc) __begin = clock(); ccc; cerr<<"Time of work = "<<(clock()-__begin)/CLOCKS_PER_SEC<<endl;

#define mp make_pair
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef int itn;

template<class T1, class T2, class T3>
struct triple{ T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c){} };
template<class T1, class T2, class T3>
bool operator<(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a<t2.a;else if(t1.b!=t2.b)return t1.b<t2.b;else return t1.c<t2.c;}
template<class T1, class T2, class T3>
bool operator>(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a>t2.a;else if(t1.b!=t2.b)return t1.b>t2.b;else return t1.c>t2.c;}
#define tri triple<int,int,int>
#define trll triple<ll,ll,ll>

#define FI(n) for(int i=0;i<n;++i)
#define FJ(n) for(int j=0;j<n;++j)
#define FK(n) for(int k=0;k<n;++k)
#define FL(n) for(int l=0;l<n;++l)
#define FQ(n) for(int q=0;q<n;++q)
#define FOR(i,a,b) for(int i = (a), __e = (int) (b); i < __e; ++i)
#define all(a) std::begin(a), std::end(a)
#define reunique(v) v.resize(unique(v.begin(), v.end()) - v.begin())

#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(1.0 * (x))
#define pow(x, n) pow(1.0 * (x), n)

#define COMPARE(obj) [&](const std::decay<decltype(obj)>::type& a, const std::decay<decltype(obj)>::type& b)
#define COMPARE_BY(obj, field) [&](const std::decay<decltype(obj)>::type& a, const std::decay<decltype(obj)>::type& b) { return a.field < b.field; }

#define checkbit(n, b) (((n) >> (b)) & 1)
#define setbit(n, b) ((n) | (static_cast<std::decay<decltype(n)>::type>(1) << (b)))
#define removebit(n, b) ((n) & ~(static_cast<std::decay<decltype(n)>::type>(1) << (b)))
#define flipbit(n, b) ((n) ^ (static_cast<std::decay<decltype(n)>::type>(1) << (b)))
inline int bits_count(int v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
inline int bits_count(ll v){int t=v>>32;int p=(v& ((1LL << 32) - 1)); return bits_count(t) + bits_count(p); }
unsigned int reverse_bits(register unsigned int x){ x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
template<class T>
inline int sign(T x){ return x > 0 ? 1 : x < 0 ? -1 : 0; }
inline bool isPowerOfTwo(int x){ return (x != 0 && (x&(x - 1)) == 0); }
template<class T1, class T2, class T3> T1 inline clamp(T1 x, const T2& a, const T3& b) { if (x < a) return a; else if (x > b) return b; else return x; }
unsigned long long rdtsc() {
#ifdef _MSC_VER
    return 0;
#else
    unsigned long long ret;
    __asm__ __volatile__("rdtsc" : "=A" (ret) : :);
    return ret;
#endif
}
// Fast IO ********************************************************************************************************
template<class T = int> T read_int() {
    const int B = 4096;
    static char b[B + 16], *e = b + B, *i = e;
    auto c = [&]() { if (i == e) memset(b, 0, B), cin.read(b, B), i = b; };
    c();
    while (*i && (*i < '0' || *i > '9') && *i != '-') ++i;
    c();
    bool m = false;
    if (*i == '-') ++i, c(), m = true;
    T r = 0;
    while (*i >= '0' && *i <= '9') r = r * 10 + *i - '0', ++i, c();
    ++i;
    return m ? -r : r;
}
template<class T>
void write_int(T x, char endc = '\n') {
    const int B = 4096;
    static char b[B + 20], *i = b, *e = b + B;
    if (x < 0) *i++ = '-', x = -x;
    if (x == 0) *i++ = '0';
    char* s = i;
    while (x) {
        T t = x / 10;
        char c = x - 10 * t + '0';
        *i++ = c;
        x = t;
    }
    char* f = i - 1;
    while (s < f) swap(*s, *f), ++s, --f;
    if (i > e) cout.write(b, i - b), i = b;
    *i++ = endc;
    struct F { ~F() { if (i != b) cout.write(b, i - b); } };
    static F flush;
}

//STL output *****************************************************************************************************
#define TT1 template<class T>
#define TT1T2 template<class T1, class T2>
#define TT1T2T3 template<class T1, class T2, class T3>
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p);
TT1 ostream& operator << (ostream& os, const vector<T>& v);
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, const multiset<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v);
TT1T2T3 ostream& operator << (ostream& os, const map<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const multimap<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t);
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
TT1 ostream& operator << (ostream& os, const vector<T>& v){       bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){    bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }
TT1T2 void printarray(T1 a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }

//STL input *****************************************************************************************************
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t);
TT1T2 inline istream& operator >> (istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; }
TT1 inline istream& operator >> (istream& os, vector<T>& v) {
    if (v.size()) for (T& t : v) os >> t; else {
        string s; T obj; while (s.empty()) {getline(os, s); if (!os) return os;}
        stringstream ss(s); while (ss >> obj) v.push_back(obj);
    }
    return os;
}
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; }

//Pair magic *****************************************************************************************************
#define PT1T2 pair<T1, T2>
TT1T2 inline PT1T2 operator+(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); }
TT1T2 inline PT1T2& operator+=(PT1T2 &p1 , const PT1T2 &p2) { p1.first += p2.first, p1.second += p2.second; return p1; }
TT1T2 inline PT1T2 operator-(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); }
TT1T2 inline PT1T2& operator-=(PT1T2 &p1 , const PT1T2 &p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; }

#undef TT1
#undef TT1T2
#undef TT1T2T3

#define FREIN(FILE) freopen(FILE, "rt", stdin)
#define FREOUT(FILE) freopen(FILE, "wt", stdout)

template<class T> inline void normmod(T &x, T m) { x %= m; if (x < 0) x += m; }
template<class T1, class T2> inline T2 summodfast(T1 x, T1 y, T2 m) { T2 res = x + y; if (res >= m) res -= m; return res; }
template<class T1, class T2, class T3> inline void addmodfast(T1 &x, T2 y, T3 m) { x += y; if (x >= m) x -= m; }
template<class T1, class T2, class T3> inline void submodfast(T1 &x, T2 y, T3 m) { x -= y; if (x < 0) x += m; }
inline ll mulmod(ll x, ll n, ll m){ ll r = 0; normmod(x, m); normmod(n, m); while (n) { if (n & 1) addmodfast(r, x, m); addmodfast(x, x, m); n /= 2; } return r; }
inline ll mulmodfast(ll x, ll n, ll m){ ll r = 0; while (n){ if (n & 1) addmodfast(r, x, m); addmodfast(x, x, m); n /= 2; } return r; }
inline ll powmod(ll x, ll n, ll m){ ll r = 1; normmod(x, m); while (n){ if (n & 1)r = (r*x) % m; x = (x*x) % m; n /= 2; }return r; }
inline ll powmulmod(ll x, ll n, ll m) { ll res = 1; normmod(x, m); while (n){ if (n & 1)res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; }
template<class T> inline T gcd(T a, T b) { while (b) { a %= b; T t = a; a = b; b = t; } return a; }
inline ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }
template<class T> inline T gcd(T a, T b, T c){ return gcd(gcd(a, b), c); }
ll gcdex(ll a, ll b, ll& x, ll& y) {
    if (!a) { x = 0; y = 1; return b; }
    ll y1; ll d = gcdex(b % a, a, y, y1); x = y1 - (b / a) * y;
    return d;
}
template<class T> bool isPrime(T x) { if (x < 2) return 0; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return 0; return 1; }
bool millerRabin(long long n) {
    if (n <= 2) return n == 2;
    long long s = n - 1; int t = 0; while (s % 2 == 0) s /= 2, ++t;
    for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) { if (a >= n) return a == n;
        long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue;
        for (int i = 1; i < t; ++i) if ((f = mulmodfast(f, f, n)) == n - 1) goto nextp;
        return false; nextp:;
    } return true;
}

// Useful constants

//int some_primes[7] = {24443, 100271, 1000003, 1000333, 5000321, 98765431, 1000000123};
#define INF         1011111111
#define LLINF       1000111000111000111LL
#define EPS         (double)1e-10
#define mod         1000000007
#define PI          3.14159265358979323846264
#define link        asaxlajrewqwe
#define rank        wahayawehasdakw
//*************************************************************************************

int32_t solve();
int32_t main() {
    ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef LOCAL
//    FREIN("input.txt");
//    FREOUT("out.txt");
#endif
    return solve();
}

int total = 0;
int a[301010];
int n;
int last = -1;
int prevLast = -1;
int indices[3];
bool check(int s, int mask) {
    bool skip[3];
    FI(3) {
        skip[i] = mask % 2;
        mask /= 2;
    }
    int needBonuses = total - s;
    if (needBonuses < 0) needBonuses = 0;
    int canGetBonuses = s / 10;
    if (canGetBonuses < needBonuses) return 0;

    int needFull = (needBonuses + 99) / 100;
    int canFull = s / 1000;
    int haveBonuses = 0;
    if (needFull > canFull) return 0;
    for (int i = 0; i < n; ++i) {
        if (a[i] == 2000) {
            if (s >= 2000 || (needFull > 0 && s >= 2000)) {
                needFull -= 2;
                s -= 2000;
                haveBonuses += 200;
            } else {
                if (haveBonuses + s < 2000) {
                    return 0;
                }
                int t = max(haveBonuses, 2000);
                haveBonuses -= t;
                s -= 2000 - t;
            }
        } else {
            bool sk = false;
            FJ(3) {
                if (indices[j] == i && skip[j]) {
                    sk = true;
                }
            }
            if (s >= 1000 && !sk) {
                needFull--;
                s -= 1000;
                haveBonuses += 100;
            } else {
                if (haveBonuses + s < 1000) {
                    return 0;
                }
                int t = max(haveBonuses, 1000);
                haveBonuses -= t;
                s -= 1000 - t;
            }
        }
    }
    return true;
}
int lastBefore2K = -1;
int32_t solve() {
    cin >> n;
    FI(n) {
        cin >> a[i];
        total += a[i];
        if (a[i] == 1000) {
            prevLast = last;
            last = i;
        }
    }
    for (int i = n - 1; i >= 0; --i) {
        if (a[i] == 1000) {
            lastBefore2K = i;
            break;
        }
    }
    indices[0] = lastBefore2K;
    indices[1] = last;
    indices[2] = prevLast;
    int l = 0;
    int r = INF / 10;
    while (l < r) {
        int m = (l + r) / 2;
        bool e = false;
        for (int mask = 0; mask < 8; ++mask) {
            if (check(m * 10, mask)) {
                e = true;
                break;
            }
        }
        if (e) {
            r = m;
        } else {
            l = m + 1;
        }
    }
    cout << l * 10 << endl;

    return 0;
}


#pragma comment(linker, "/stack:20000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <cmath>
# include <algorithm>
# include <stdio.h>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <random>
# include <deque>
# include <functional>
# include <iomanip>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
# include <immintrin.h>
# include <cassert>
# include <array>

using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_set>
# include <unordered_map>
# else
# include <tr1/unordered_map>
# include <tr1/unordered_set>
using namespace tr1;
# endif
# else
# include <unordered_map>
# include <unordered_set>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cerr<<#a<<"=["; printarray(a,n); cerr<<"]\n"
#define DAR(a,n,s) cerr<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cerr<<"]\n"

#ifdef _MSC_VER
#define ALIGN(x) __declspec(align(x))
#define PREFETCH(ptr, rw, level) ((void)0)
#else
#define ALIGN(x) __attribute__((aligned(x)))
#define PREFETCH(ptr, rw, level) __builtin_prefetch(ptr, rw, level)
#endif

#ifdef LOCAL
#define CURTIME() cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl
#else
#define CURTIME() ;
#endif
double __begin;
#define DTIME(ccc) __begin = clock(); ccc; cerr<<"Time of work = "<<(clock()-__begin)/CLOCKS_PER_SEC<<endl;

#define mp make_pair
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef int itn;

template<class T1, class T2, class T3>
struct triple{ T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c){} };
template<class T1, class T2, class T3>
bool operator<(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a<t2.a;else if(t1.b!=t2.b)return t1.b<t2.b;else return t1.c<t2.c;}
template<class T1, class T2, class T3>
bool operator>(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a>t2.a;else if(t1.b!=t2.b)return t1.b>t2.b;else return t1.c>t2.c;}
#define tri triple<int,int,int>
#define trll triple<ll,ll,ll>

#define FI(n) for(int i=0;i<n;++i)
#define FJ(n) for(int j=0;j<n;++j)
#define FK(n) for(int k=0;k<n;++k)
#define FL(n) for(int l=0;l<n;++l)
#define FQ(n) for(int q=0;q<n;++q)
#define FOR(i,a,b) for(int i = (a), __e = (int) (b); i < __e; ++i)
#define all(a) std::begin(a), std::end(a)
#define reunique(v) v.resize(unique(v.begin(), v.end()) - v.begin())

#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(1.0 * (x))
#define pow(x, n) pow(1.0 * (x), n)

#define COMPARE(obj) [&](const std::decay<decltype(obj)>::type& a, const std::decay<decltype(obj)>::type& b)
#define COMPARE_BY(obj, field) [&](const std::decay<decltype(obj)>::type& a, const std::decay<decltype(obj)>::type& b) { return a.field < b.field; }

#define checkbit(n, b) (((n) >> (b)) & 1)
#define setbit(n, b) ((n) | (static_cast<std::decay<decltype(n)>::type>(1) << (b)))
#define removebit(n, b) ((n) & ~(static_cast<std::decay<decltype(n)>::type>(1) << (b)))
#define flipbit(n, b) ((n) ^ (static_cast<std::decay<decltype(n)>::type>(1) << (b)))
inline int bits_count(int v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
inline int bits_count(ll v){int t=v>>32;int p=(v& ((1LL << 32) - 1)); return bits_count(t) + bits_count(p); }
unsigned int reverse_bits(register unsigned int x){ x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
template<class T>
inline int sign(T x){ return x > 0 ? 1 : x < 0 ? -1 : 0; }
inline bool isPowerOfTwo(int x){ return (x != 0 && (x&(x - 1)) == 0); }
template<class T1, class T2, class T3> T1 inline clamp(T1 x, const T2& a, const T3& b) { if (x < a) return a; else if (x > b) return b; else return x; }
unsigned long long rdtsc() {
#ifdef _MSC_VER
    return 0;
#else
    unsigned long long ret;
    __asm__ __volatile__("rdtsc" : "=A" (ret) : :);
    return ret;
#endif
}
// Fast IO ********************************************************************************************************
template<class T = int> T read_int() {
    const int B = 4096;
    static char b[B + 16], *e = b + B, *i = e;
    auto c = [&]() { if (i == e) memset(b, 0, B), cin.read(b, B), i = b; };
    c();
    while (*i && (*i < '0' || *i > '9') && *i != '-') ++i;
    c();
    bool m = false;
    if (*i == '-') ++i, c(), m = true;
    T r = 0;
    while (*i >= '0' && *i <= '9') r = r * 10 + *i - '0', ++i, c();
    ++i;
    return m ? -r : r;
}
template<class T>
void write_int(T x, char endc = '\n') {
    const int B = 4096;
    static char b[B + 20], *i = b, *e = b + B;
    if (x < 0) *i++ = '-', x = -x;
    if (x == 0) *i++ = '0';
    char* s = i;
    while (x) {
        T t = x / 10;
        char c = x - 10 * t + '0';
        *i++ = c;
        x = t;
    }
    char* f = i - 1;
    while (s < f) swap(*s, *f), ++s, --f;
    if (i > e) cout.write(b, i - b), i = b;
    *i++ = endc;
    struct F { ~F() { if (i != b) cout.write(b, i - b); } };
    static F flush;
}

//STL output *****************************************************************************************************
#define TT1 template<class T>
#define TT1T2 template<class T1, class T2>
#define TT1T2T3 template<class T1, class T2, class T3>
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p);
TT1 ostream& operator << (ostream& os, const vector<T>& v);
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, const multiset<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v);
TT1T2T3 ostream& operator << (ostream& os, const map<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const multimap<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t);
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
TT1 ostream& operator << (ostream& os, const vector<T>& v){       bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){    bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }
TT1T2 void printarray(T1 a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }

//STL input *****************************************************************************************************
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t);
TT1T2 inline istream& operator >> (istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; }
TT1 inline istream& operator >> (istream& os, vector<T>& v) {
    if (v.size()) for (T& t : v) os >> t; else {
        string s; T obj; while (s.empty()) {getline(os, s); if (!os) return os;}
        stringstream ss(s); while (ss >> obj) v.push_back(obj);
    }
    return os;
}
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; }

//Pair magic *****************************************************************************************************
#define PT1T2 pair<T1, T2>
TT1T2 inline PT1T2 operator+(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); }
TT1T2 inline PT1T2& operator+=(PT1T2 &p1 , const PT1T2 &p2) { p1.first += p2.first, p1.second += p2.second; return p1; }
TT1T2 inline PT1T2 operator-(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); }
TT1T2 inline PT1T2& operator-=(PT1T2 &p1 , const PT1T2 &p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; }

#undef TT1
#undef TT1T2
#undef TT1T2T3

#define FREIN(FILE) freopen(FILE, "rt", stdin)
#define FREOUT(FILE) freopen(FILE, "wt", stdout)

template<class T> inline void normmod(T &x, T m) { x %= m; if (x < 0) x += m; }
template<class T1, class T2> inline T2 summodfast(T1 x, T1 y, T2 m) { T2 res = x + y; if (res >= m) res -= m; return res; }
template<class T1, class T2, class T3> inline void addmodfast(T1 &x, T2 y, T3 m) { x += y; if (x >= m) x -= m; }
template<class T1, class T2, class T3> inline void submodfast(T1 &x, T2 y, T3 m) { x -= y; if (x < 0) x += m; }
inline ll mulmod(ll x, ll n, ll m){ ll r = 0; normmod(x, m); normmod(n, m); while (n) { if (n & 1) addmodfast(r, x, m); addmodfast(x, x, m); n /= 2; } return r; }
inline ll mulmodfast(ll x, ll n, ll m){ ll r = 0; while (n){ if (n & 1) addmodfast(r, x, m); addmodfast(x, x, m); n /= 2; } return r; }
inline ll powmod(ll x, ll n, ll m){ ll r = 1; normmod(x, m); while (n){ if (n & 1)r = (r*x) % m; x = (x*x) % m; n /= 2; }return r; }
inline ll powmulmod(ll x, ll n, ll m) { ll res = 1; normmod(x, m); while (n){ if (n & 1)res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; }
template<class T> inline T gcd(T a, T b) { while (b) { a %= b; T t = a; a = b; b = t; } return a; }
inline ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }
template<class T> inline T gcd(T a, T b, T c){ return gcd(gcd(a, b), c); }
ll gcdex(ll a, ll b, ll& x, ll& y) {
    if (!a) { x = 0; y = 1; return b; }
    ll y1; ll d = gcdex(b % a, a, y, y1); x = y1 - (b / a) * y;
    return d;
}
template<class T> bool isPrime(T x) { if (x < 2) return 0; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return 0; return 1; }
bool millerRabin(long long n) {
    if (n <= 2) return n == 2;
    long long s = n - 1; int t = 0; while (s % 2 == 0) s /= 2, ++t;
    for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) { if (a >= n) return a == n;
        long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue;
        for (int i = 1; i < t; ++i) if ((f = mulmodfast(f, f, n)) == n - 1) goto nextp;
        return false; nextp:;
    } return true;
}

// Useful constants

//int some_primes[7] = {24443, 100271, 1000003, 1000333, 5000321, 98765431, 1000000123};
#define INF         1011111111
#define LLINF       1000111000111000111LL
#define EPS         (double)1e-10
#define mod         1000000007
#define PI          3.14159265358979323846264
#define link        asaxlajrewqwe
#define rank        wahayawehasdakw
//*************************************************************************************

int32_t solve();
int32_t main() {
    ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef LOCAL
    FREIN("input.txt");
    //    FREOUT("out.txt");
#endif
    return solve();
}

int total = 0;
int a[301010];
int n;
int last = -1;
int prevLast = -1;
int indices[3];
bool check(int s, int mask) {
    bool skip[3];
    FI(3) {
        skip[i] = mask % 2;
        mask /= 2;
    }
    int needBonuses = total - s;
    if (needBonuses < 0) needBonuses = 0;
    int canGetBonuses = s / 10;
    if (canGetBonuses < needBonuses) return 0;

    int needFull = (needBonuses + 99) / 100;
    int canFull = s / 1000;
    int haveBonuses = 0;
    if (needFull > canFull) return 0;
    for (int i = 0; i < n; ++i) {
//        DBN(i, haveBonuses, s);
        if (a[i] == 2000) {
            if (s >= 2000 || (needFull > 0 && s >= 2000)) {
                needFull -= 2;
                s -= 2000;
                haveBonuses += 200;
            } else {
                if (haveBonuses + s < 2000) {
                    return 0;
                }
                int t = min(haveBonuses, 2000);
                haveBonuses -= t;
                s -= 2000 - t;
            }
        } else {
            bool sk = false;
            FJ(3) {
                if (indices[j] == i && skip[j]) {
                    sk = true;
                }
            }
            if (s >= 1000 && !sk) {
                needFull--;
                s -= 1000;
                haveBonuses += 100;
            } else {
                if (haveBonuses + s < 1000) {
                    return 0;
                }
                int t = min(haveBonuses, 1000);
                haveBonuses -= t;
                s -= 1000 - t;
            }
        }
    }
    return true;
}
int lastBefore2K = -1;
int32_t solve() {
    cin >> n;
    FI(n) {
        cin >> a[i];
        total += a[i];
        if (a[i] == 1000) {
            prevLast = last;
            last = i;
        }
    }
    for (int i = n - 1; i >= 0; --i) {
        if (a[i] == 1000) {
            lastBefore2K = i;
            break;
        }
    }
    indices[0] = lastBefore2K;
    indices[1] = last;
    indices[2] = prevLast;
    int l = 0;
    int r = INF / 10;
    while (l < r) {
        int m = (l + r) / 2;
        bool e = false;
        for (int mask = 0; mask < 8; ++mask) {
            if (check(m * 10, mask)) {
                e = true;
                break;
            }
        }
        if (e) {
            r = m;
        } else {
            l = m + 1;
        }
    }
//    for (int mask = 0; mask < 8; ++mask) {
//        DBN(mask, check(10000, mask));
//    }
    cout << l * 10 << endl;

    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX=1<<29;
int main(){
    int n,dpa[300],dpb[300],t,ans=MAX;
    scanf("%d",&n);
    dpa[0]=0;
    for(int i=1;i<300;i++) dpa[i]=MAX;
    while(n--){
        scanf("%d",&t);
        for(int i=0;i<300;i++) dpb[i]=MAX;
        if(t==1000){
            for(int i=0;i<=30;i++)
                for(int j=i+1;j<=30&&j<=i+10;j++)
                    dpb[i]=min(dpb[i],dpa[j]+(10-j+i));
            for(int i=1;i<=30;i++)
                dpb[i]=min(dpb[i],dpa[i-1]+10);
        }else{
            for(int i=0;i<=30;i++)
                for(int j=i+1;j<=30&&j<=i+20;j++)
                    dpb[i]=min(dpb[i],dpa[j]+(20-j+i));
            for(int i=2;i<=30;i++)
                dpb[i]=min(dpb[i],dpa[i-2]+20);
        }
        memcpy(dpa,dpb,sizeof(dpa));
    }
    for(int i=0;i<=30;i++)
        ans=min(ans,dpa[i]);
    printf("%d00\n",ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX=1<<29;
int main(){
    int n,dpa[300],dpb[300],t,ans=MAX;
    scanf("%d",&n);
    dpa[0]=0;
    for(int i=1;i<300;i++) dpa[i]=MAX;
    while(n--){
        scanf("%d",&t);
        for(int i=0;i<300;i++) dpb[i]=MAX;
        if(t==1000){
            for(int i=0;i<=250;i++)
                for(int j=i+1;j<=250&&j<=i+10;j++)
                    dpb[i]=min(dpb[i],dpa[j]+(10-j+i));
            for(int i=1;i<=250;i++)
                dpb[i]=min(dpb[i],dpa[i-1]+10);
        }else{
            for(int i=0;i<=250;i++)
                for(int j=i+1;j<=250&&j<=i+20;j++)
                    dpb[i]=min(dpb[i],dpa[j]+(20-j+i));
            for(int i=2;i<=250;i++)
                dpb[i]=min(dpb[i],dpa[i-2]+20);
        }
        memcpy(dpa,dpb,sizeof(dpa));
    }
    for(int i=0;i<=250;i++)
        ans=min(ans,dpa[i]);
    printf("%d00\n",ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX=1<<29;
int main(){
    int n,dpa[30],dpb[30],t,ans=MAX;
    scanf("%d",&n);
    dpa[0]=0;
    for(int i=1;i<30;i++) dpa[i]=MAX;
    while(n--){
        scanf("%d",&t);
        for(int i=0;i<30;i++) dpb[i]=MAX;
        if(t==1000){
            for(int i=0;i<=21;i++)
                for(int j=i+1;j<=21&&j<=i+10;j++)
                    dpb[i]=min(dpb[i],dpa[j]+(10-j+i));
            for(int i=1;i<=21;i++)
                dpb[i]=min(dpb[i],dpa[i-1]+10);
        }else{
            for(int i=0;i<=21;i++)
                for(int j=i+1;j<=21&&j<=i+20;j++)
                    dpb[i]=min(dpb[i],dpa[j]+(20-j+i));
            for(int i=2;i<=21;i++)
                dpb[i]=min(dpb[i],dpa[i-2]+20);
        }
        memcpy(dpa,dpb,sizeof(dpa));
    }
    for(int i=0;i<=21;i++)
        ans=min(ans,dpa[i]);
    printf("%d00\n",ans);
    return 0;
}


#include<iostream>
using namespace std;
int main() {
	int n,a,sum=0,tot=0,ans;
	cin>>n;
	for(int i=1; i<=n; ++i) {
		cin>>a;
		sum+=a/1000;
		if(a==1000)	tot++;
	}
	ans=10*sum/11;
	if(sum<=11) ans=sum-a/1000;
	if(tot==0 && ans%2) ans--;
	cout<<1000*sum-ans*100;
	return 0;
}


#include <stdio.h>
#include <iostream>
using namespace std;
int read() {
	char x;
	x=getchar();
	while(x<'1'|| x>'2') x=getchar();
	return x^48;
}
int main() {
	int n,a,sum=0,ans;
	cin>>n;
	for(int i=1; i<=n; ++i) {
		a=read();
		sum+=a;
	}
	ans=10*sum/11;
	if(sum<=11) ans=sum-a;
	if(2*n==sum && ans%2) ans--;
	cout<<10*sum-ans<<"00";
	return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))

using namespace std;

const int N = 300000 + 5;
const int K = 25 ;
const int inf = 1e9+7;
int d[N][K+1];
int a[N];

int dp(int n){
    d[0][0]=0;
    for(int i=1;i<=K;++i){
        d[0][i]=inf;
    }
    for(int i=1;i<=n;++i){
        for(int j=0;j<=K;++j){
            d[i][j]=inf;
            if(j-a[i]/10>=0){
                d[i][j]=min(d[i-1][j-a[i]/10]+a[i],d[i][j]);
            }
            for(int k=j+1;k<=K&&(k-j)<=a[i];++k){
                d[i][j]=min(d[i][j],d[i-1][k]+a[i]-(k-j));
            }
        }
    }
    int ans=inf;
    for(int i=0;i<=K;++i){
        ans=min(ans,d[n][i]);
    }
    return ans;
}

int main(){
    //freopen("/home/lu/code/r.txt","r",stdin);
    //freopen("/home/lu/code/w.txt","w",stdout);
    int n;
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;++i){
            scanf("%d",&a[i]);
            a[i]/=100;
        }
        printf("%d\n",dp(n)*100);
    }
    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))

using namespace std;

const int N = 300000 + 5;
const int K = 35 ;
const int inf = 1e9+7;
int d[N][K+1];
int a[N];

int dp(int n){
    d[0][0]=0;
    for(int i=1;i<=K;++i){
        d[0][i]=inf;
    }
    for(int i=1;i<=n;++i){
        for(int j=0;j<=K;++j){
            d[i][j]=inf;
            if(j-a[i]/10>=0){
                d[i][j]=min(d[i-1][j-a[i]/10]+a[i],d[i][j]);
            }
            for(int k=j+1;k<=K&&(k-j)<=a[i];++k){
                d[i][j]=min(d[i][j],d[i-1][k]+a[i]-(k-j));
            }
        }
    }
    int ans=inf;
    for(int i=0;i<=K;++i){
        ans=min(ans,d[n][i]);
    }
    return ans;
}

int main(){
    //freopen("/home/lu/code/r.txt","r",stdin);
    //freopen("/home/lu/code/w.txt","w",stdout);
    int n;
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;++i){
            scanf("%d",&a[i]);
            a[i]/=100;
        }
        printf("%d\n",dp(n)*100);
    }
    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))

using namespace std;

const int N = 300000 + 5;
const int K = 21;
const int inf = 1e9+7;
int d[N][K+1];
int a[N];

int dp(int n){
    d[0][0]=0;
    for(int i=1;i<=K;++i){
        d[0][i]=inf;
    }
    for(int i=1;i<=n;++i){
        for(int j=0;j<=K;++j){
            d[i][j]=inf;
            if(j>=a[i]/10){
                d[i][j]=min(d[i-1][j-a[i]/10]+a[i],d[i][j]);
            }
            for(int k=j+1;k<=K;++k){
                d[i][j]=min(d[i][j],d[i-1][k]+a[i]-(k-j));
            }
        }
    }
    int ans=inf;
    for(int i=0;i<=K;++i){
        ans=min(ans,d[n][i]);
    }
    return ans;
}

int main(){
    //freopen("/home/lu/code/r.txt","r",stdin);
    //freopen("/home/lu/code/w.txt","w",stdout);
    int n;
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;++i){
            scanf("%d",&a[i]);
            a[i]/=100;
        }
        printf("%d\n",dp(n)*100);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;

const int mod = 1e9 + 7;
const int maxn = 3e5 + 5;

int add(int x,int y) {
    x += y;
    if(x >= mod) x -= mod;
    return x;
}

int mul(int x,int y) {
    LL z = 1LL * x * y;
    return z - z / mod * mod;
}

int a[maxn],b[maxn];
int f[2][55];

void updata(int &x,int y) {
    x = max(x,y);
}

int main(){
    int n,i,j,u,v,s;
    scanf("%d",&n);
    for(i = 1,s = 0;i <= n; i++) {
        scanf("%d",&a[i]);
        b[i] = a[i] / 100;
        a[i] /= 1000;
        s += a[i];
    }
    u = 0;
    f[0][0] = 1;
    for(i = 1;i <= n; i++) {
        v = u ^ 1;
        memset(f[v],0,sizeof(f[v]));
        for(j = 0;j <= 20; j++) {
            if(f[u][j]) {
                if(j < b[i]) updata(f[v][0],f[u][j] + j);
                else updata(f[v][j - b[i]],f[u][j] + b[i]);
                if(j + a[i] <= 20) updata(f[v][j + a[i]],f[u][j]);
            }
        }
        u ^= 1;
    }
    int x = 1;
    for(i = 0;i <= 20; i++) x = max(x,f[u][i]);
    x--;
    printf("%d\n",s * 1000 - x * 100);
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<ll>::iterator sit;
typedef map<ll,ll>::iterator mit;

const int C = 200;
int dp[2][C+1];
int a[300011];
const int INF = int(1e9);
int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n; cin>>n;
	//n=240000;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		//if(i<100000) a[i]=2000;
		//else a[i]=1000;
		a[i]/=100;
	}
	for(int i=0;i<=C;i++) dp[0][i]=dp[1][i]=INF;
	dp[0][a[0]/10] = a[0];
	for(int i=1;i<n;i++)
	{
		int cur=(i&1); int pre=(cur^1);
		for(int j=0;j<=C;j++) dp[cur][j]=INF;
		for(int j=0;j<=C;j++)
		{
			if(dp[pre][j]<INF)
			{
				int mn = min(a[i], j);
				dp[cur][j-mn] = min(dp[cur][j-mn], dp[pre][j] + a[i] - mn);
				if(j+(a[i]/10)<=C) dp[cur][j+(a[i]/10)]=min(dp[cur][j+(a[i]/10)],dp[pre][j]+a[i]);
			}
		}
	}
	int ans=INF;
	for(int i=0;i<=C;i++)
	{
		ans=min(ans,dp[(n+1)&1][i]);
	}
	ll res = ans*100;
	cout<<res<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;

const int mod = 1e9 + 7;
const int maxn = 3e5 + 5;

int add(int x,int y) {
    x += y;
    if(x >= mod) x -= mod;
    return x;
}

int mul(int x,int y) {
    LL z = 1LL * x * y;
    return z - z / mod * mod;
}

int a[maxn],b[maxn];
int f[2][55];

void updata(int &x,int y) {
    x = max(x,y);
}

int main(){
    int n,i,j,u,v,s;
    scanf("%d",&n);
    for(i = 1,s = 0;i <= n; i++) {
        scanf("%d",&a[i]);
        b[i] = a[i] / 100;
        a[i] /= 1000;
        s += a[i];
    }
    u = 0;
    f[0][0] = 1;
    for(i = 1;i <= n; i++) {
        v = u ^ 1;
        memset(f[v],0,sizeof(f[v]));
        for(j = 0;j <= 40; j++) {
            if(f[u][j]) {
                if(j < b[i]) updata(f[v][0],f[u][j] + j);
                else updata(f[v][j - b[i]],f[u][j] + b[i]);
                if(j + a[i] <= 40) updata(f[v][j + a[i]],f[u][j]);
            }
        }
        u ^= 1;
    }
    int x = 1;
    for(i = 0;i <= 40; i++) x = max(x,f[u][i]);
    x--;
    printf("%d\n",s * 1000 - x * 100);
    return 0;
}


#include<algorithm>
#include<cstdio>
#include<cstring>
#define F1(i,l,r) for(int i=l;i<=r;++i)
#define F2(i,r,l) for(int i=r;i>=l;--i)
#define sf(i) scanf("%d",&i)
#define pf(i) printf("%d\n",i)
#define N 300070
#define inf 0x3f3f3f3f

int n;
int a[N],dp[N][47];

int main(){
	sf(n);
	F1(i,1,n) sf(a[i]),a[i]/=100;
	std::memset(dp,0x3f,sizeof dp);
	dp[1][0]=0;
	for(int i=1;i<=n;++i){
		for(int j=0;j<=30;++j){
			if(dp[i][j]!=inf){
				if(dp[i+1][j+a[i]/10]!=inf)
					dp[i+1][j+a[i]/10]=std::min(dp[i+1][j+a[i]/10],dp[i][j]+a[i]);
				else
					dp[i+1][j+a[i]/10]=dp[i][j]+a[i];
				int v=std::min(j,a[i]);
				if(dp[i+1][j-v]!=inf)
					dp[i+1][j-v]=std::min(dp[i+1][j-v],dp[i][j]+a[i]-v);
				else
					dp[i+1][j-v]=dp[i][j]+a[i]-v;
			}
		}
	}
	int ans=inf;
	for(int i=0;i<=30;++i)
		if(dp[n+1][i]!=inf) ans=std::min(ans,dp[n+1][i]*100);
	pf(ans);
}


#include<algorithm>
#include<cstdio>
#include<cstring>
#define F1(i,l,r) for(int i=l;i<=r;++i)
#define F2(i,r,l) for(int i=r;i>=l;--i)
#define sf(i) scanf("%d",&i)
#define pf(i) printf("%d\n",i)
#define N 300070
#define inf 0x3f3f3f3f

int n;
int a[N],dp[N][40];

int main(){
	sf(n);
	F1(i,1,n) sf(a[i]),a[i]/=100;
	std::memset(dp,-1,sizeof dp);
	dp[1][0]=0;
	for(int i=1;i<=n;++i){
		for(int j=0;j<=40;++j){
			if(~dp[i][j]){
				if(~dp[i+1][j+a[i]/10])
					dp[i+1][j+a[i]/10]=std::min(dp[i+1][j+a[i]/10],dp[i][j]+a[i]);
				else
					dp[i+1][j+a[i]/10]=dp[i][j]+a[i];
				int v=std::min(j,a[i]);
				if(~dp[i+1][j-v])
					dp[i+1][j-v]=std::min(dp[i+1][j-v],dp[i][j]+a[i]-v);
				else
					dp[i+1][j-v]=dp[i][j]+a[i]-v;
			}
		}
	}
	int ans=inf;
	for(int i=0;i<=40;++i)
		if(~dp[n+1][i]) ans=std::min(ans,dp[n+1][i]*100);
	pf(ans);
}


#include <bits/stdc++.h>

inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - 48;
		ch = getchar();
	}
	return x * f;
}

const int N = 3e5 + 7;
int n, a[N];

int main() {
	n = read();
	int tmp = 0, ans = 0;
	for (int i = 1; i <= n; ++ i) {
		a[i] = read();
	}
	for (int i = 1; i <= n - 1; ++ i) {
		if (tmp >= a[i]) {
			tmp -= a[i];
		} else {
			tmp += a[i] / 10;
			ans += a[i];
		}
	}
	if (a[n] == 2000) {
		ans += a[n] - tmp;
	} else {
		if (tmp <= 1000) ans += a[n] - tmp;
		else ans += a[n] - tmp + a[n - 1] / 10;
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>

inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - 48;
		ch = getchar();
	}
	return x * f;
}

const int N = 3e5 + 7;
int n, a[N];

int main() {
	n = read();
	int tmp = 0, ans = 0;
	int sum = 0;
	for (int i = 1; i <= n; ++ i) {
		a[i] = read();
		sum += a[i];
		tmp += (a[i] == 1000);
	}
	sum /= 1000;
	int flag = sum * 10 / 11;
	if(sum <= 11) flag = sum - a[n] / 1000;
	if(!tmp && flag & 1) flag--;
	ans = sum * 1000 - flag * 100;
	printf("%d\n", ans);
	return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
#define ll long long
#define N 200005
#define Abs(x) ((x) > 0 ? (x) : -(x))
#define Min(x,y) ((x) < (y) ? (x) : (y))
#define Max(x,y) ((x) > (y) ? (x) : (y))
inline ll read(){
	ll x = 0,f = 1;char ch = getchar();
	while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}
	while(ch >= '0' && ch <= '9'){x = x*10+ch-'0';ch = getchar();}
	return x*f;
}

ll n;
ll num[3];
ll sum,ans,x;

int main(){
	n = read();
	for(int i = 1;i <= n;i++){
		x = read();
		num[x/1000]++;
		sum += x/1000;
	}
	ans = 10*sum/11;
	if(sum <= 11)
		ans = ans-x/1000+1;
	if(ans%2 == 1 && num[1] == 0)
		ans--;
	printf("%I64d\n",sum*1000-ans*100);
	return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
#define ll long long
#define N 200005
#define Abs(x) ((x) > 0 ? (x) : -(x))
#define Min(x,y) ((x) < (y) ? (x) : (y))
#define Max(x,y) ((x) > (y) ? (x) : (y))
inline ll read(){
	ll x = 0,f = 1;char ch = getchar();
	while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}
	while(ch >= '0' && ch <= '9'){x = x*10+ch-'0';ch = getchar();}
	return x*f;
}

ll n;
ll num[3];
ll sum,ans,x;

int main(){
	n = read();
	for(int i = 1;i <= n;i++){
		x = read();
		num[x/1000]++;
		sum += x/1000;
	}
	ans = 10*sum/11;
	if(sum < 11)
		ans = ans-x/1000+1;
	if(ans%2 == 1 && num[1] == 0)
		ans--;
	printf("%I64d\n",sum*1000-ans*100);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<ctime>
#include<set>
#include<vector>
#include<map>
#include<queue>

#define N 300005
#define M 105

#define ls (t<<1)
#define rs ((t<<1)|1)
#define mid ((l+r)>>1)

#define mk make_pair
#define pb push_back
#define fi first
#define se second

using namespace std;

int i,j,m,n,p,k,a[N];

long long f[N][M];

const long long inf=(long long)1e18;

int main()
{
		memset(f,60,sizeof(f));
		scanf("%d",&n);
		for (i=0;i<n;++i)
		{
				scanf("%d",&a[i]);
				a[i]/=1000;
		}
		f[0][0]=0;
		for (i=0;i<n;++i)
			for (j=0;j<M;++j)
				if (f[i][j]<inf)
				{
						if (j+a[i]<M) f[i+1][j+a[i]]=min(f[i+1][j+a[i]],f[i][j]+a[i]*10);
						if (a[i]*10<=j) f[i+1][j-a[i]*10]=min(f[i+1][j-a[i]*10],f[i][j]);
						else f[i+1][0]=min(f[i+1][0],f[i][j]+(a[i]*10-j));
				}
		long long ans=(long long)1e18;
		for (i=0;i<M;++i)
			ans=min(ans,min((long long)2e16,f[n][i])*100);
		printf("%I64d\n",ans);
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<ctime>
#include<set>
#include<vector>
#include<map>
#include<queue>

#define N 300005
#define M 105

#define ls (t<<1)
#define rs ((t<<1)|1)
#define mid ((l+r)>>1)

#define mk make_pair
#define pb push_back
#define fi first
#define se second

using namespace std;

int i,j,m,n,p,k,a[N];

long long f[N][M];

const long long inf=(long long)1e18;

int main()
{
		memset(f,60,sizeof(f));
		scanf("%d",&n);
		for (i=0;i<n;++i)
		{
				scanf("%d",&a[i]);
				a[i]/=1000;
		}
		f[0][0]=0;
		for (i=0;i<n;++i)
			for (j=0;j<M;++j)
				if (f[i][j]<inf)
				{
						if (j+a[i]<M) f[i+1][j+a[i]]=min(f[i+1][j+a[i]],f[i][j]+a[i]*10);
						if (a[i]*10<=j) f[i+1][j-a[i]]=min(f[i+1][j-a[i]*10],f[i][j]);
						else f[i+1][0]=min(f[i+1][0],f[i][j]+(a[i]*10-j));
				}
		long long ans=(long long)1e18;
		for (i=0;i<M;++i)
			ans=min(ans,min((long long)2e16,f[n][i])*100);
		printf("%I64d\n",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
#define ll long long
#define M 1000000007
#define all(a) a.begin(), a.end()

const int B = 100;
int n, x;
int f[B + 1], g[B + 1];

int main(){
	scanf("%d", &n);
	memset(f, 127, sizeof(f));
	f[0] = 0;
	for(int i = 1; i <= n; ++i){
		scanf("%d", &x); x /= 100;
		memcpy(g, f, sizeof(g));
		memset(f, 127, sizeof(f));
		for(int j = 0; j <= B; ++j)
			if(g[j] < 1e9){
				int p = min(j + x / 10, B);
				f[p] = min(f[p], g[j] + x);
				int t = min(x, j);
				f[j - t] = min(f[j - t], g[j] + x - t);
			}
	}
	int ans = 1e9;
	for(int i = 0; i <= B; ++i)
		ans = min(ans, f[i]);
	printf("%d\n", ans * 100);

#ifndef ONLINE_JUDGE
	system("pause");
#endif
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
#define ll long long
#define M 1000000007
#define all(a) a.begin(), a.end()

const int B = 100;
int n, x;
int f[B + 1], g[B + 1];

int main(){
	scanf("%d", &n);
	memset(f, 127, sizeof(f));
	f[0] = 0;
	for(int i = 1; i <= n; ++i){
		scanf("%d", &x); x /= 100;
		memcpy(g, f, sizeof(g));
		memset(f, 127, sizeof(f));
		for(int j = 0; j <= B; ++j)
			if(g[j] < 1e9){
				if(j + x / 10 <= B) f[j + x / 10] = min(f[j + x / 10], g[j] + x);
				int t = min(x, j);
				f[j - t] = min(f[j - t], g[j] + x - j);
			}
	}
	int ans = 1e9;
	for(int i = 0; i <= B; ++i)
		ans = min(ans, f[i]);
	printf("%d\n", ans * 100);

#ifndef ONLINE_JUDGE
	system("pause");
#endif
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
#define ll long long
#define M 1000000007
#define all(a) a.begin(), a.end()

const int B = 100;
int n, x;
int f[B + 1], g[B + 1];

int main(){
	scanf("%d", &n);
	memset(f, 127, sizeof(f));
	f[0] = 0;
	for(int i = 1; i <= n; ++i){
		scanf("%d", &x); x /= 100;
		memcpy(g, f, sizeof(g));
		memset(f, 127, sizeof(f));
		for(int j = 0; j <= B; ++j)
			if(g[j] < 1e9){
				int p = min(j + x / 10, B);
				f[p] = min(f[p], g[j] + x);
				int t = min(x, j);
				f[j - t] = min(f[j - t], g[j] + x - j);
			}
	}
	int ans = 1e9;
	for(int i = 0; i <= B; ++i)
		ans = min(ans, f[i]);
	printf("%d\n", ans * 100);

#ifndef ONLINE_JUDGE
	system("pause");
#endif
	return 0;
}


#include<bits/stdc++.h>

const int N = 300010;
const int M = 30;
const int INF = 0x3f3f3f3f;

int dp[N][M], a[N];
int n;

int main(){
	scanf("%d", &n);
	for (int i = 1; i <= n; ++ i){
		scanf("%d", &a[i]);
		a[i] /= 100;
	}
	memset(dp, 0x3f, sizeof(dp));
	dp[0][0] = 0;
	for (int i = 1; i <= n; ++ i){
		for (int j = 0; j < M; ++ j){
			if (dp[i - 1][j] == INF){
				continue;
			}
			if (a[i] / 10 + j < M){
				dp[i][j + a[i] / 10] = std::min(dp[i][j + a[i] / 10], dp[i - 1][j] + a[i]);
			}
			int x = std::min(a[i], j);
			dp[i][j - x] = std::min(dp[i][j - x], dp[i - 1][j] + a[i] - x);
		}
	}
	int min = INT_MAX;
	for (int i = 0; i < M; ++ i){
		min = std::min(min, dp[n][i]);
	}
	return printf("%d\n", min * 100), 0;
}


#include<bits/stdc++.h>

const int N = 300010;
const int M = 300;
const int INF = 0x3f3f3f3f;

int dp[N][M], a[N];
int n;

int main(){
	scanf("%d", &n);
	for (int i = 1; i <= n; ++ i){
		scanf("%d", &a[i]);
		a[i] /= 100;
	}
	memset(dp, 0x3f, sizeof(dp));
	dp[0][0] = 0;
	for (int i = 1; i <= n; ++ i){
		for (int j = 0; j < M; ++ j){
			if (dp[i - 1][j] == INF){
				continue;
			}
			if (a[i] / 10 + j < M){
				dp[i][j + a[i] / 10] = std::min(dp[i][j + a[i] / 10], dp[i - 1][j] + a[i]);
			}
			int x = std::min(a[i], j);
			dp[i][j - x] = std::min(dp[i][j - x], dp[i - 1][j] + a[i] - x);
		}
	}
	int min = INT_MAX;
	for (int i = 0; i < M; ++ i){
		min = std::min(min, dp[n][i]);
	}
	return printf("%d\n", min * 100), 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define REP(i,a,b) for (int i = a ; i <= b ; i++)
#define X real()
#define Y imag()
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;

const int OO = INT_MAX;
const int MOD = 1e9+7;
const int N = 1e2+7;

int cnt[N];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    int n,m;
    cin >> n >> m;

    REP(i,0,n-1)
    {
        int ai;
        cin >> ai;
        cnt[ai]+=2;
    }

    int ans = 0;
    vector<pi> vec;
    REP(i,1,m)
    {
        REP(j,1,m)
        {
            if (i==j) continue;
            while(cnt[j] > 0 && cnt[i] > 0)
            {
                vec.PB({i,j});
                cnt[i]--;
                cnt[j]--;
                ans++;
            }
        }
    }

    REP(i,1,m)
    {
        while (cnt[i] > 0)
        {
            cnt[i]-=2;
            vec.PB({i,i});
        }
    }
    cout << ans << endl;
    REP(i,0,n-1)cout << vec[i].F << ' ' << vec[i].S << endl;
    return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define REP(i,a,b) for (int i = a ; i <= b ; i++)
#define X real()
#define Y imag()
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;

const int OO = INT_MAX;
const int MOD = 1e9+7;
const int N = 1e2+7;

int cntr[N];
int cntl[N];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    int n,m;
    cin >> n >> m;

    REP(i,0,n-1)
    {
        int ai;
        cin >> ai;
        cntl[ai]++;
        cntr[ai]++;
    }

    int ans = 0;
    vector<pi> vec;
    REP(i,1,m)
    {
        REP(j,i+1,m+i)
        {
            j%=m;
            if (i==j) continue;
            while(cntl[i] > 0 && cntr[j] > 0)
            {
                vec.PB({i,j});
                cntl[i]--;
                cntr[j]--;
                ans++;
            }
        }
    }

    REP(i,1,m)
    {
        while (cntl[i] > 0 && cntr[i] > 0)
        {
            cntl[i]--;
            cntr[i]--;
            vec.PB({i,i});
        }
    }
    cout << ans << endl;
    REP(i,0,n-1)cout << vec[i].F << ' ' << vec[i].S << endl;
    return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define REP(i,a,b) for (int i = a ; i <= b ; i++)
#define X real()
#define Y imag()
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;

const int OO = INT_MAX;
const int MOD = 1e9+7;
const int N = 1e2+7;

pi cnt[N];
//int cnt[N];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    int n,m;
    cin >> n >> m;
    REP(i,1,m) cnt[i].S = i;
    REP(i,0,n-1)
    {
        int ai;
        cin >> ai;
        cnt[ai-1].F++;
        //cntr[ai]++;
    }
    vector<int> vec1,vec2;
    sort(cnt,cnt+m+1,greater<pi>());
    //cout << cnt[1] << endl;
    REP(i,0,m-1) REP(j,0,cnt[i].F-1) vec1.PB(cnt[i].S);
    int cnt = 0;
    REP(i,0,n-1)
    {
        if (vec1[i] == vec1[0]) cnt++; else vec2.PB(vec1[i]);
    }
    REP(i,0,cnt-1) vec2.PB(vec1[0]);
    //reverse(vec2.begin(),vec2.end());
    vector<pi> v;
    int ans = 0;

    REP(i,0,n-1)
    {
        if (vec1[i] != vec2[i]) ans++;
        v.PB({vec1[i],vec2[i]});
    }

    cout << ans << endl;
    REP(i,0,n-1)cout << v[i].F+1 << ' ' << v[i].S+1 << endl;
    return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define REP(i,a,b) for (int i = a ; i <= b ; i++)
#define X real()
#define Y imag()
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;

const int OO = INT_MAX;
const int MOD = 1e9+7;
const int N = 1e2+7;

int cntr[N];
int cntl[N];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    int n,m;
    cin >> n >> m;

    REP(i,0,n-1)
    {
        int ai;
        cin >> ai;
        cntl[ai]++;
        cntr[ai]++;
    }

    int ans = 0;
    vector<pi> vec;
    REP(i,1,m)
    {
        REP(k,i+1,m+i)
        {
            int j =k%(m+1);
            //cout << j << endl;
            if (i==j) continue;
            while(cntl[i] > 0 && cntr[j] > 0)
            {
                vec.PB({i,j});
                cntl[i]--;
                cntr[j]--;
                ans++;
            }
        }
    }

    REP(i,1,m)
    {
        while (cntl[i] > 0 && cntr[i] > 0)
        {
            cntl[i]--;
            cntr[i]--;
            vec.PB({i,i});
        }
    }
    cout << ans << endl;
    REP(i,0,n-1)cout << vec[i].F << ' ' << vec[i].S << endl;
    return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define REP(i,a,b) for (int i = a ; i <= b ; i++)
#define X real()
#define Y imag()
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;

const int OO = INT_MAX;
const int MOD = 1e9+7;
const int N = 1e2+7;

pi cnt[N];
//int cnt[N];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    int n,m;
    cin >> n >> m;
    REP(i,1,m) cnt[i].S = i;
    REP(i,0,n-1)
    {
        int ai;
        cin >> ai;
        cnt[ai].F++;
        //cntr[ai]++;
    }
    vector<int> vec1,vec2;
    sort(cnt,cnt+m+1);
    //cout << cnt[1] << endl;
    REP(i,1,m) REP(j,0,cnt[i].F-1) vec1.PB(cnt[i].S);
    vec2 = vec1;
    reverse(vec2.begin(),vec2.end());
    vector<pi> v;
    int ans = 0;

    REP(i,0,n-1)
    {
        if (vec1[i] != vec2[i]) ans++;
        v.PB({vec1[i],vec2[i]});
    }

    cout << ans << endl;
    REP(i,0,n-1)cout << v[i].F << ' ' << v[i].S << endl;
    return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define REP(i,a,b) for (int i = a ; i <= b ; i++)
#define X real()
#define Y imag()
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;

const int OO = INT_MAX;
const int MOD = 1e9+7;
const int N = 1e2+7;

pi cnt[N];
//int cnt[N];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    int n,m;
    cin >> n >> m;
    REP(i,1,m) cnt[i].S = i;
    REP(i,0,n-1)
    {
        int ai;
        cin >> ai;
        cnt[ai].F++;
        //cntr[ai]++;
    }
    vector<int> vec1,vec2;
    sort(cnt,cnt+m+1);
    //cout << cnt[1] << endl;
    REP(i,1,m) REP(j,0,cnt[i].F-1) vec1.PB(cnt[i].S);
    int cnt = 0;
    REP(i,0,n-1)
    {
        if (vec1[i] == vec1[0]) cnt++; else vec2.PB(vec1[i]);
    }
    REP(i,0,cnt-1) vec2.PB(vec1[0]);
    //reverse(vec2.begin(),vec2.end());
    vector<pi> v;
    int ans = 0;

    REP(i,0,n-1)
    {
        if (vec1[i] != vec2[i]) ans++;
        v.PB({vec1[i],vec2[i]});
    }

    cout << ans << endl;
    REP(i,0,n-1)cout << v[i].F << ' ' << v[i].S << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
typedef long long ll;
#define MAX 200105
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
bool debug = 1;
int n,m, k;
//int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk, lm;

int a[MAX], K, low[MAX], high[MAX], ans[MAX];
void pp() {
    printf("%d\n", K);
    REP(i,n) printf("%d ",ans[i]);
}
bool ck() {
    if (K<=0) return 0;
    REP(i,n) if (a[i]+1>K) return 0;
    REP(i,K+1) {
        low[i]=0; high[i]=n;
    }
    high[0]=0; low[K]=n;
    REP(i,n) if (a[i]!=-1) {
        high[a[i]] = min(high[a[i]], i);
        low[a[i]+1] = max(low[a[i]+1], i+1);
    }
    REP(i,K) low[i+1] = max(low[i+1], low[i]+2);
    for(int i=K;i;i--) {
        low[i-1] = max(low[i-1], low[i]-5);
    }
    REP(i,K+1) if (low[i]>high[i]) return 0;
    REP(i,K) {
        for(int j=low[i];j<low[i+1];j++) {
            ans[j]=i+1;
        }
    }return 1;
}
int main(){
    scanf("%d", &n);
    REP(i,n) {
        scanf("%d", a+i); a[i]--;
    }
    int i;
    for(i=n-1;i>=0;i--) if (a[i] != -1) break;
    int used = i==-1 ? 0 : a[i]+1;
    used += (n-i)/2;
    for(i=3;i>=-3;i--) {
        K = used+i;
        if (ck()) {
            pp(); return 0;
        }
    }
    puts("-1");
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define info(msg) cout << (#msg) << " = " << (msg) << endl;
#define print(msg) cout << (msg) << endl;
#define forv(var, initial, n) for (int (var) = (initial); (var) < (n); (++var))
#define pb push_back
#define fr first
#define sc second
#define prev preww

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;

const int INF = 2e9;
const long long LINF = 9e18;
const int MOD1 = 2e9 + 11;
const int MOD2 = 1e9 + 9;
const int MOD3 = 1e9 + 7;
const int p = 37;

int n;
int m;
int x[200001];
int l[200001];
int r[200001];

int out (int i, int j) {
	set<int> u;
	for (int k = 1; k <= 5; k++) {
		if (x[i - k] != 0) u.insert (x[i - k]);
		if (k >= 2 && l[i - k] <= j - 1 && r[i - k] >= j - 1)
			if (u.empty () || (sz (u) == 1 && *u.begin () == j)) {
				out (i - k, j - 1);
				for (int t = 0; t < k; t++) printf ("%d ", j);
				break;
			}
	}
	re 0;
}

int main () {
	scanf ("%d", &n);
	for (int i = 0; i < n; i++) scanf ("%d", &x[i]);
	for (int i = 0; i < n; i++)	l[i + 1] = 1e9, r[i + 1] = -1e9;
	l[0] = r[0] = 0;
	for (int i = 0; i < n; i++)
		if (l[i] <= r[i]) {
			set<int> u;
			for (int j = 0; j < 5 && i + j < n; j++) {
				if (x[i + j] != 0) u.insert (x[i + j]);
				if (j >= 1) {
					if (u.empty ()) {
						l[i + j + 1] = min (l[i + j + 1], l[i] + 1);
						r[i + j + 1] = max (r[i + j + 1], r[i] + 1);
					} else
					if (sz (u) == 1) {
						int tmp = *u.begin ();
						if (tmp - 1 >= l[i] && tmp - 1 <= r[i]) {
							l[i + j + 1] = min (l[i + j + 1], tmp);
							r[i + j + 1] = max (r[i + j + 1], tmp);
						}
					}
				}
			}
		}
	if (l[n] > r[n]) printf ("-1\n"); else {
		printf ("%d\n", r[n]);
		out (n, r[n]);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define info(msg) cout << (#msg) << " = " << (msg) << endl;
#define forv(var, initial, n) for (int (var) = (initial); (var) < (n); (++var))
#define pb push_back
#define fr first
#define sc second
#define prev preww

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;

const int INF = 2e9;
const long long LINF = 9e18;
const int MOD1 = 2e9 + 11;
const int MOD2 = 1e9 + 9;
const int MOD3 = 1e9 + 7;
const int p = 37;

const int MAXN = 2e5 + 10;
pii dp[MAXN];
int vis[MAXN], A[MAXN], n;

pii rec(int idx){
    if(idx == 0) return pii(0,0);
    if(idx == 1) return pii(INF, -1);
    if(vis[idx])  return dp[idx];
    vis[idx] = 1;

    int num = 0;
    dp[idx] = pii(INF, -1);
    for(int j = idx, len = 1; len <= 5 && j >= 1; len++, j--){
        if(A[j] != 0){
            if (num != 0 && A[j] != num ) break;
            else { num = A[j]; }
        }

        if(len == 1) continue;

        dp[j-1] = rec(j-1);
        if(dp[j-1].first == INF) continue;

        int l = INF, r = -1;
        if(num == 0){ // start a new block here ...
            l = dp[j-1].first+1, r = dp[j-1].second+1;
        }
        else if(dp[j-1].first <= num -1  && num - 1 <= dp[j-1].second){
            l = r = num;
        }
        dp[idx].first = min(dp[idx].first, l);
        dp[idx].second = max(dp[idx].second, r);
    }
    return dp[idx];
}

void print(int idx, int num){
    if(num == 0) return;
    for(int j = idx, len = 1; j >= 1 && len <= 5; len++, j--){
        if(len == 1) continue;
        if(dp[j-1].first <= num-1 && num-1 <= dp[j-1].second){
            print(j-1, num-1);
            while(len--){
                printf("%d ", num);
            }
            return;
        }
    }
}
int main()
{

    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> A[i];
    pii ret = rec(n);
    cout << ret.second << endl;
    if(ret.second != -1){
        print(n, ret.second);
        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <bitset>
#include <random>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define vi vector<int>
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

typedef long long ll;
typedef short inth;

const int N = 2e5;
const int M = N + 7;
const int MOD = 1e9 + 7;
const ll INF = 1e16 + 17;

const int K = 5;

void ex() {
    printf("-1\n");
    exit(0);
}

int a[M];
int l[M], r[M], len[M];

int an[M];

bool dp[M][K + 3];
pii par[M][K + 3];

bool cmp(int i, int j) {
    return l[i] < l[j];
}

void fillLR(int tl, int tr, int cnt, int st) {
    if (tl > tr)
        return;
    // cerr << "FILL: " << tl << ' ' << tr << " | " << cnt << ' ' << st << "\n";
    int d = tr - tl + 1;
    int cur = d / cnt;
    for (int i = st; i < st + cnt; ++i) {
        int x = cur + (d % cnt > i - st);
        while (x-- && tl <= tr)
            an[tl++] = i;
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        l[a[i]] = l[a[i]] ? l[a[i]] : i;
        r[a[i]] = i;
    }
    vi ord;
    for (int i = 1; i <= N; ++i) {
        if (l[i]) {
            len[i] = r[i] - l[i] + 1;
            ord.pb(i);
        }
        if (len[i] > 5)
            ex();
    }
    l[0] = -4;
    r[0] = 0;
    len[0] = K;
    ord.pb(0);

    sort(all(ord), cmp);
    for (int t = 1; t < sz(ord); ++t) {
        if (l[ord[t]] <= r[ord[t - 1]])
            ex();
    }
    dp[0][0] = true;
    int m = sz(ord) - 1;
    for (int t = 1; t <= m; ++t) {
        int i = ord[t];
        int j = ord[t - 1];
        for (int rr = 0; rr + len[j] <= K; ++rr) {
            if (!dp[t - 1][rr])
                continue;
            for (int tr = 0; tr + len[i] <= K; ++tr) {
                for (int tl = 0; tl + tr + len[i] <= K; ++tl) {
                    if (tr + tl + len[i] < 2)
                        continue;
                    int dist = (l[i] - tl) - (r[j] + rr) - 1;
                    int cnt = i - j - 1;
                    if (cnt * 2 <= dist && dist <= cnt * 5) {
                        dp[t][tr] = true;
                        par[t][tr] = mp(tl, rr);
                    }
                }
            }
        }
    }
    int ans = 0, k = 0;
    for (int tr = 0; tr + len[ord[m]] <= K && r[ord[m]] + tr <= n; ++tr) {
        if (dp[m][tr]) {
            int len = n - r[ord[m]] - tr;
            if (len != 1) {
                fillLR(r[ord[m]] + tr + 1, n, len / 2, ord[m] + 1);
                k = tr;
                ans = ord[m] + len / 2;
                break;
            }
        }
    }
    if (!ans)
        ex();

    while (m) {
        int cnt = ord[m] - ord[m - 1] - 1;
        pii cur = par[m][k];
        fillLR(l[ord[m]] - cur.fi, r[ord[m]] + k, 1, ord[m]);
        fillLR(r[ord[m - 1]] + cur.se + 1, l[ord[m]] - cur.fi - 1, cnt, ord[m - 1] + 1);
        m--;
        k = cur.se;
    }

    printf("%d\n", ans);
    for (int i = 1; i <= n; ++i)
        printf("%d ", an[i]);
    printf("\n");
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <bitset>
#include <random>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define vi vector<int>
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

typedef long long ll;
typedef short inth;

const int N = 2e5;
const int M = N + 7;
const int MOD = 1e9 + 7;
const ll INF = 1e16 + 17;

const int K = 5;

void ex() {
    printf("-1\n");
    exit(0);
}

int a[M];
int l[M], r[M], len[M];

int an[M];

bool dp[M][K + 3];
pii par[M][K + 3];

bool cmp(int i, int j) {
    return l[i] < l[j];
}

void fillLR(int tl, int tr, int cnt, int st) {
    if (tl > tr)
        return;
    // cerr << "FILL: " << tl << ' ' << tr << " | " << cnt << ' ' << st << "\n";
    int d = tr - tl + 1;
    int cur = d / cnt;
    for (int i = st; i < st + cnt; ++i) {
        int x = cur + (d % cnt > i - st);
        while (x-- && tl <= tr)
            an[tl++] = i;
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        l[a[i]] = l[a[i]] ? l[a[i]] : i;
        r[a[i]] = i;
    }
    vi ord;
    for (int i = 1; i <= n; ++i) {
        if (l[i]) {
            len[i] = r[i] - l[i] + 1;
            ord.pb(i);
        }
        if (len[i] > 5)
            ex();
    }
    l[0] = -4;
    r[0] = 0;
    len[0] = K;
    ord.pb(0);

    sort(all(ord), cmp);
    for (int t = 1; t < sz(ord); ++t) {
        if (l[ord[t]] <= r[ord[t - 1]])
            ex();
    }
    dp[0][0] = true;
    int m = sz(ord) - 1;
    for (int t = 1; t <= m; ++t) {
        int i = ord[t];
        int j = ord[t - 1];
        for (int rr = 0; rr + len[j] <= K; ++rr) {
            if (!dp[t - 1][rr])
                continue;
            for (int tr = 0; tr + len[i] <= K; ++tr) {
                for (int tl = 0; tl + tr + len[i] <= K; ++tl) {
                    if (tr + tl + len[i] < 2)
                        continue;
                    int dist = (l[i] - tl) - (r[j] + rr) - 1;
                    int cnt = i - j - 1;
                    if (cnt * 2 <= dist && dist <= cnt * 5) {
                        dp[t][tr] = true;
                        par[t][tr] = mp(tl, rr);
                    }
                }
            }
        }
    }
    int ans = 0, k = 0;
    for (int tr = 0; tr + len[ord[m]] <= K && r[ord[m]] + tr <= n; ++tr) {
        if (dp[m][tr]) {
            int len = n - r[ord[m]] - tr;
            if (len != 1) {
                fillLR(r[ord[m]] + tr + 1, n, len / 2, ord[m] + 1);
                k = tr;
                ans = ord[m] + len / 2;
                break;
            }
        }
    }
    if (!ans)
        ex();

    while (m) {
        int cnt = ord[m] - ord[m - 1] - 1;
        pii cur = par[m][k];
        fillLR(l[ord[m]] - cur.fi, r[ord[m]] + k, 1, ord[m]);
        fillLR(r[ord[m - 1]] + cur.se + 1, l[ord[m]] - cur.fi - 1, cnt, ord[m - 1] + 1);
        m--;
        k = cur.se;
    }

    printf("%d\n", ans);
    for (int i = 1; i <= n; ++i)
        printf("%d ", an[i]);
    printf("\n");
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <bitset>
#include <random>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define vi vector<int>
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

typedef long long ll;
typedef short inth;

const int N = 2e5;
const int M = N + 7;
const int MOD = 1e9 + 7;
const ll INF = 1e16 + 17;

const int K = 5;

void ex() {
    printf("-1\n");
    exit(0);
}

int a[M];
int l[M], r[M], len[M];

int an[M];

bool dp[M][K + 3];
pii par[M][K + 3];

bool cmp(int i, int j) {
    return l[i] < l[j];
}

void fillLR(int tl, int tr, int cnt, int st) {
    if (tl > tr)
        return;
    int d = tr - tl + 1;
    int cur = (d + cnt - 1) / cnt;
    for (int i = st; i < st + cnt; ++i) {
        int x = cur;
        while (x-- && tl <= tr)
            an[tl++] = i;
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        l[a[i]] = l[a[i]] ? l[a[i]] : i;
        r[a[i]] = i;
    }
    vi ord;
    for (int i = 1; i <= n; ++i) {
        if (l[i]) {
            len[i] = r[i] - l[i] + 1;
            ord.pb(i);
        }
        if (len[i] > 5)
            ex();
    }
    l[0] = -4;
    r[0] = 0;
    len[0] = K;
    ord.pb(0);

    sort(all(ord), cmp);
    for (int t = 1; t < sz(ord); ++t) {
        if (l[ord[t]] <= r[ord[t - 1]])
            ex();
    }
    dp[0][0] = true;
    int m = sz(ord) - 1;
    for (int t = 1; t <= m; ++t) {
        int i = ord[t];
        int j = ord[t - 1];
        for (int rr = 0; rr + len[j] <= K; ++rr) {
            if (!dp[t - 1][rr])
                continue;
            for (int tr = 0; tr + len[i] <= K; ++tr) {
                for (int tl = 0; tl + tr + len[i] <= K; ++tl) {
                    if (tr + tl + len[i] < 2)
                        continue;
                    int dist = (l[i] - tl) - (r[j] + rr) - 1;
                    int cnt = i - j - 1;
                    if (cnt * 2 <= dist && dist <= cnt * 5) {
                        dp[t][tr] = true;
                        par[t][tr] = mp(tl, rr);
                    }
                }
            }
        }
    }
    int ans = 0, k = 0;
    for (int tr = 0; tr + len[ord[m]] <= K && r[ord[m]] + tr <= n; ++tr) {
        if (dp[m][tr]) {
            int len = n - r[ord[m]] - tr;
            if (len != 1) {
                fillLR(r[ord[m]] + tr + 1, n, len / 2, ord[m] + 1);
                k = tr;
                ans = ord[m] + len / 2;
                break;
            }
        }
    }
    if (!ans)
        ex();

    while (m) {
        int cnt = ord[m] - ord[m - 1] - 1;
        pii cur = par[m][k];
        fillLR(l[ord[m]] - cur.fi, r[ord[m]] + k, 1, ord[m]);
        fillLR(r[ord[m - 1]] + cur.se + 1, l[ord[m]] - cur.fi - 1, cnt, ord[m - 1] + 1);
        m--;
        k = cur.se;
    }

    printf("%d\n", ans);
    for (int i = 1; i <= n; ++i)
        printf("%d ", an[i]);
    printf("\n");
    return 0;
}


//370E
//6/8/20

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
int N;
vector<ii> rec;
vector<int> space;
vector<ii> bounds;
int memo[200005][5];
ii path[200005][5];

vector<int> fill(int length, int start, int end){
//    cout<<"FILL "<<length<<" "<<start<<" "<<end<<endl;
    vector<int> ANS;
    int totalNum = end-start+1;
    if (totalNum > 0){
        int avg = length/totalNum;
        int rem = length - totalNum*avg;
        for (int j = start; j < start+rem; j++){
           for (int k = 0; k < avg+1; k++) ANS.push_back(j);
        }
        for (int j = start+rem; j <= end; j++){
           for (int k = 0; k < avg; k++) ANS.push_back(j);
        }
    }
    return ANS;
}

int main(int argc, const char * argv[]) {
    cin >> N;

    rec.push_back(ii(0, 0)); int ct = 0;
    for (int i = 0, x; i < N; i++){
        cin >> x;
        if (x == 0) ct++;
        else{
            if (rec[rec.size()-1].first > x){
                cout<<-1<<endl; return 0;
            }

            if (rec[rec.size()-1].first == x){
                rec[rec.size()-1].second += ct+1;
                ct = 0;
                continue;
            }

            int betweenLow = max(0, 2*(x-1-rec[rec.size()-1].first));
            int betweenHigh = min(ct, 5*(x-1-rec[rec.size()-1].first));
            if (betweenLow > ct){
                cout<<-1<<endl; return 0;
            }
            bounds.push_back(ii(ct-betweenHigh, ct-betweenLow));

            rec.push_back(ii(x,1));
            space.push_back(ct);
            ct = 0;
        }
    }
//    rec.push_back(ii(200005, 0));
    space.push_back(ct);
//    cout<<"REC "<<endl;
//    for (auto p:rec) cout<<p.first<<" "<<p.second<<endl;
//    cout<<"SPACE "<<endl;
//    for(auto p:space) cout<<p<<endl;
//    cout<<"BOUNDS "<<endl;
//    for (auto p : bounds) cout<<p.first<<" "<<p.second<<endl;

    for (int i = 0; i < 200005; i++) for (int j = 0; j < 5; j++) memo[i][j] = -1;
    for (int i = (int)rec.size(); i >= 1; i--)for (int j = 0; j < 5; j++){
        //memo[i][j];
        if (i == rec.size()){
            int rem = space[space.size()-1] - j;
            if (rem >= 0){
                memo[i][j] = rem == 1 ? -1 :rec[i-1].first+rem/2;
            }
        }else{
            for (int b = 0; b < 5; b++) for (int a = 0; a <= 5-b; a++){ //before, after
                if (b+a+rec[i].second >= 2 && b+a+rec[i].second <= 5 && b+j >= bounds[i-1].first && b+j <= bounds[i-1].second){
                    if (memo[i+1][a] > memo[i][j]){
                        memo[i][j] = memo[i+1][a];
                        path[i][j] = ii(b, a);
                    }
                }
            }
        }
//        cout<<"memo "<<i<<" "<<j<<" "<<memo[i][j]<<endl;
    }

    cout<<memo[1][0]<<endl;
    if (memo[1][0] == -1) return 0;
    vector<int> ANS;
//    vector<int> fillV = fill(space[0]-path[)
    int prev = 0;
    for (int i = 1; i <= (int)rec.size(); i++){
        ii opt = path[i][prev];
//        cout<<" End "<<i<<" prev "<<prev<<" "<<opt.first<<" "<<opt.second<<endl;

        //Fill in middle numbers
//        cout<<"FILL before "<<space[i-1]<<" "<<prev<<" "<<opt.first<<endl;
        vector<int> fillV = fill( space[i-1]-prev-opt.first, rec[i-1].first+1, i==(int)rec.size()? memo[1][0] : rec[i].first-1 );
        for (auto x : fillV) ANS.push_back(x);

        //Fill in boundary numbers
        if (i != (int)rec.size()){
            for (int j = 0; j < opt.first+opt.second+rec[i].second; j++) ANS.push_back(rec[i].first);

        }

        prev = opt.second;
    }
    for (int i = 0; i < ANS.size(); i++) cout<<ANS[i]<<(i == ANS.size()-1?"\n":" ");

//    int remAmt = N-(int) ANS.size();
//    for (int j = rec[rec.size()-1].first+1; j <= memo[1][0]; j++)for (int k = 0; k < 2; k++){
//        ANS.push_back(j);
//    }
//    if (remAmt %2 == 0) ANS.push_back(memo[1][0]);
//
//    for (int i = 0; i < ANS.size(); i++) cout<<ANS[i]<<(i == ANS.size()-1?"\n":" ");

    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

#include <x86intrin.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(x) x.begin(), x.end()

#define mp make_pair
#define in insert
#define er erase
#define sc scanf
#define pr printf
#define con continue
#define pb push_back
#define sc scanf
#define pr printf
#define up upper_bound
#define lb lower_bound
#define s second
#define f first
#define int long long

const long long infl = 1e18;
const int N = 3e5 + 300, N2 = 2e3, inf = 1e9 + 200;

set <int> S;
vector <int> g[N];
int n, m;
int a[N], l[N], r[N], mx;
string ss;

int dfs(int v, int ll, int rr){
//	cout << v << " " << ll << " " << rr << endl;
	rr = min(rr, l[v]);
	int tol = max(ll + 1, r[v]), tor = rr + 4;
	if (ll > rr || tol > tor){
//		cout << v << " << " << endl;
		return -1;
	}
	int p = dfs(v + 1, tol + 1, tor + 1) - 1;
	if (p > 0){
		int q = max(ll, p - 4);
		for (int i = q; i <= p; i++){
			a[i] = v;
		}
		return q;
	}
	if (ll <= n+1 && rr >= n+1 && v > mx){
		return n + 1;
	}
	return -1;
}

main (){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	fill(l, l+N, n+1);
	fill(r, r+N, -1);
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		if (!l[a[i]]) l[a[i]] = i;
		r[a[i]] = i;
		mx = max(a[i], mx);
	}

	int x = dfs(1,1,1);
	if (x < 0){
		cout << -1;
	}
	else{
		cout << a[n] << endl;
		for (int i = 1; i <= n; i++){
			cout << a[i] << " ";
		}
	}
	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

#include <x86intrin.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(x) x.begin(), x.end()

#define mp make_pair
#define in insert
#define er erase
#define sc scanf
#define pr printf
#define con continue
#define pb push_back
#define sc scanf
#define pr printf
#define up upper_bound
#define lb lower_bound
#define s second
#define f first
#define int long long

const long long infl = 1e18;
const int N = 3e5 + 300, N2 = 2e3, inf = 1e9 + 200;

set <int> S;
vector <int> g[N];
int n, m;
int a[N], l[N], r[N], mx;
string ss;

int dfs(int v, int ll, int rr){
	rr = min(rr, l[v]);
	int tol = max(ll + 1, r[v]), tor = rr + 4;
	if (ll > rr || tol > tor){
		return -1;
	}
	int p = dfs(v + 1, tol + 1, tor + 1) - 1;
	if (p > 0){
		int q = max(ll, p - 4);
		for (int i = q; i <= p; i++){
			a[i] = v;
		}
		return q;
	}
	if (ll <= n+1 && rr >= n+1 && v > mx){
		return n + 1;
	}
	return -1;
}

main (){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	fill(l, l+N, n+1);
	fill(r, r+N, -1);
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		if (a[i]){
			if (!l[a[i]]) l[a[i]] = i;
			r[a[i]] = i;
			mx = max(a[i], mx);
		}
	}

	int x = dfs(1,1,1);
	if (x <= 0){
		cout << -1;
	}
	else{
		cout << a[n] << endl;
		for (int i = 1; i <= n; i++){
			cout << a[i] << " ";
		}
	}
	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

#include <x86intrin.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(x) x.begin(), x.end()

#define mp make_pair
#define in insert
#define er erase
#define sc scanf
#define pr printf
#define con continue
#define pb push_back
#define sc scanf
#define pr printf
#define up upper_bound
#define lb lower_bound
#define s second
#define f first
#define int long long

const long long infl = 1e18;
const int N = 3e5 + 300, N2 = 2e3, inf = 1e9 + 200;

set <int> S;
vector <int> g[N];
int n, m;
int a[N], l[N], r[N], mx;
string ss;

int dfs(int v, int ll, int rr){
	rr = min(rr, l[v]);
	int tol = max(ll + 1, r[v]), tor = rr + 4;
	if (ll > rr || tol > tor){
		return -1;
	}
	int p = dfs(v + 1, tol + 1, tor + 1) - 1;
	if (p > 0){
		int q = max(ll, p - 4);
		for (int i = q; i <= p; i++){
			a[i] = v;
		}
		return q;
	}
	if (ll <= n+1 && rr >= n+1 && v > mx){
		return n + 1;
	}
	return -1;
}

main (){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	fill(l, l+N, n+1);
	fill(r, r+N, -1);
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		if (a[i]){
			l[a[i]] = min(l[a[i]], i);
			r[a[i]] = max(r[a[i]], i);
			mx = max(a[i], mx);
		}
	}
	int x = dfs(1,1,1);
	if (x <= 0){
		cout << -1;
	}
	else{
		cout << a[n] << endl;
		for (int i = 1; i <= n; i++){
			cout << a[i] << " ";
		}
	}
	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

#include <x86intrin.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, std::less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(x) x.begin(), x.end()

#define mp make_pair
#define in insert
#define er erase
#define sc scanf
#define pr printf
#define con continue
#define pb push_back
#define sc scanf
#define pr printf
#define up upper_bound
#define lb lower_bound
#define s second
#define f first
#define int long long

const long long infl = 1e18;
const int N = 3e5 + 300, N2 = 2e3, inf = 1e9 + 200;

set <int> S;
vector <int> g[N];
int n, m;
int a[N], l[N], r[N], mx;
string ss;

int dfs(int v, int ll, int rr){
	rr = min(rr, l[v]);
	int tol = max(ll + 1, r[v]), tor = rr + 4;
	if (ll > rr || tol > tor){
		return -1;
	}
	int p = dfs(v + 1, tol + 1, tor + 1) - 1;
	if (p > 0){
		int q = max(ll, p - 4);
		for (int i = q; i <= p; i++){
			a[i] = v;
		}
		return q;
	}
	if (ll <= n+1 && rr >= n+1 && v > mx){
		return n + 1;
	}
	return -1;
}

main (){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	fill(l, l+N, n+1);
	fill(r, r+N, -1);
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		if (!l[a[i]]) l[a[i]] = i;
		r[a[i]] = i;
		mx = max(a[i], mx);
	}

	int x = dfs(1,1,1);
	if (x <= 0){
		cout << -1;
	}
	else{
		cout << a[n] << endl;
		for (int i = 1; i <= n; i++){
			cout << a[i] << " ";
		}
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>

using namespace std;

class p {
public:
	int l, r, c;
	p(int l, int r, int c)
		:l(l), r(r), c(c)
	{}
};

bool cmp(const p &i, const p &j) {
	return i.l < j.l;
}

int a[200002], n;
vector<p> v;
int l[100001], r[100001];
int d[6][100001];

int f(int h, int k) {
	if (k < 0) return 1;
	if (d[h][k] >= 0) return d[h][k];
	if (v[k + 1].l - h <= v[k].r) return d[h][k] = 0;
	for (int i = v[k].l - 4; i <= v[k].l; i++) {
		for (int j = v[k].r; j <= v[k].r + 4; j++) {
			int cl = i, cr = j, nl = v[k + 1].l - h;
			if (cl<1 || cr>n || cr >= nl) continue;
			if (cr - cl + 1 > 5 || cr - cl + 1 < 2) continue;
			int c1 = (nl - cr + 3) / 5, c2 = (nl - cr - 1) / 2;
			if (v[k].c + c1 + 1 > v[k + 1].c || v[k].c + c2 + 1 < v[k + 1].c) continue;
			if (f(v[k].l - i, k - 1)) return d[h][k] = 1;
		}
	}
	return d[h][k] = 0;
}

void trace(int h, int k) {
	if (k < 0) return;
	for (int i = v[k].l - 4; i <= v[k].l; i++) {
		for (int j = v[k].r; j <= v[k].r + 4; j++) {
			int cl = i, cr = j, nl = v[k + 1].l - h;
			if (cl<1 || cr>n || cr >= nl) continue;
			if (cr - cl + 1 > 5 || cr - cl + 1 < 2) continue;
			int c1 = (nl - cr + 3) / 5, c2 = (nl - cr - 1) / 2;
			if (v[k].c + c1 + 1 > v[k + 1].c || v[k].c + c2 + 1 < v[k + 1].c) continue;
			if (f(v[k].l - i, k - 1)) {
				for (int p = i; p <= j; p++)
					a[p] = v[k].c;
				int need = (v[k + 1].c - v[k].c - 1), len = nl - cr - 1;
				if (need) {
					int p = len / need, q = len%need, sum = 0;
					int cur = v[k].c + 1;
					for (int i = cr + 1; i < nl; i++) {
						a[i] = cur;
						sum++;
						if (sum == p) {
							if (q) q--;
							else {
								sum = 0; cur++;
							}
						}
						else if (sum > p) {
							sum = 0;
							cur++;
						}
					}
				}
				trace(v[k].l - i, k - 1);
				return;
			}
		}
	}
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		if (i == 1) {
			if (a[i] > 1) {
				puts("-1");
				return 0;
			}
			a[i] = 1;
		}
		if (a[i]) {
			if (!l[a[i]]) l[a[i]] = i;
			r[a[i]] = i;
		}
	}
	for (int i = 1; i <= 100000; i++) if (l[i]) {
		v.push_back(p(l[i], r[i], i));
	}
	if (v.size() == 1) {
		if (v[0].r - v[0].l + 1 > 5) {
			puts("-1");
			return 0;
		}
		if (v[0].r == 1) {
			a[2] = 1;
			v[0].r++;
		}
		for (int i = v[0].l; i <= v[0].r; i++)
			a[i] = 1;
		int rem = (n - v[0].r) / 2;
		int cur = 2;
		for (int i = v[0].r + 1; i < n; i += 2) {
			a[i] = a[i + 1] = cur;
			cur++;
		}
		if (a[n] == 0) a[n] = a[n - 1];
		printf("%d\n", a[n]);
		for (int i = 1; i <= n; i++)
			printf("%d ", a[i]);
		puts("");
		return 0;
	}
	sort(v.begin(), v.end(), cmp);
	for (int i = 0; i < v.size(); i++) {
		if (v[i].r - v[i].l + 1 > 5) {
			puts("-1");
			return 0;
		}
		if (i && v[i].l <= v[i - 1].r) {
			puts("-1");
			return 0;
		}
		if (i && v[i].c < v[i - 1].c) {
			puts("-1");
			return 0;
		}
		if (i > 0 && i < v.size() - 1 && v[i].l == v[i].r && v[i - 1].r == v[i].l - 1 && v[i + 1].l == v[i].r + 1) {
			puts("-1");
			return 0;
		}
	}
	for (int i = 0; i < v.size(); i++) {
		for (int j = v[i].l; j <= v[i].r; j++)
			a[j] = v[i].c;
	}
	memset(d, -1, sizeof(d));
	for (int l = v.back().l - 4; l <= v.back().l; l++) {
		for (int r = v.back().r; r <= v.back().r + 4; r++) {
			if (r - l + 1 > 5 || r - l + 1 < 2) continue;
			if (l<1 || r>n) continue;
			if (f(v.back().l - l, v.size() - 2)) {
				if (r == n - 1 && r - l + 1 == 5) continue;
				trace(v.back().l - l, v.size() - 2);
				for (int j = l; j <= r; j++)
					a[j] = v.back().c;
				int cur = v.back().c + 1;
				for (int j = r + 1; j < n; j += 2) {
					a[j] = a[j + 1] = cur;
					cur++;
				}
				if (a[n] == 0) a[n] = a[n - 1];
				printf("%d\n", a[n]);
				for (int j = 1; j <= n; j++)
					printf("%d ", a[j]);
				puts("");
				return 0;
			}
		}
	}
	puts("-1");
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>

using namespace std;

class p {
public:
	int l, r, c;
	p(int l, int r, int c)
		:l(l), r(r), c(c)
	{}
};

bool cmp(const p &i, const p &j) {
	return i.l < j.l;
}

int a[200002], n;
vector<p> v;
int l[100001], r[100001];
int d[6][100001];

int f(int h, int k) {
	if (k < 0) return 1;
	if (d[h][k] >= 0) return d[h][k];
	if (v[k + 1].l - h <= v[k].r) return d[h][k] = 0;
	for (int i = v[k].l - 4; i <= v[k].l; i++) {
		for (int j = v[k].r; j <= v[k].r + 4; j++) {
			int cl = i, cr = j, nl = v[k + 1].l - h;
			if (cl<1 || cr>n || cr >= nl) continue;
			if (cr - cl + 1 > 5 || cr - cl + 1 < 2) continue;
			int c1 = (nl - cr + 3) / 5, c2 = (nl - cr - 1) / 2;
			if (v[k].c + c1 + 1 > v[k + 1].c || v[k].c + c2 + 1 < v[k + 1].c) continue;
			if (f(v[k].l - i, k - 1)) return d[h][k] = 1;
		}
	}
	return d[h][k] = 0;
}

void trace(int h, int k) {
	if (k < 0) return;
	for (int i = v[k].l - 4; i <= v[k].l; i++) {
		for (int j = v[k].r; j <= v[k].r + 4; j++) {
			int cl = i, cr = j, nl = v[k + 1].l - h;
			if (cl<1 || cr>n || cr >= nl) continue;
			if (cr - cl + 1 > 5 || cr - cl + 1 < 2) continue;
			int c1 = (nl - cr + 3) / 5, c2 = (nl - cr - 1) / 2;
			if (v[k].c + c1 + 1 > v[k + 1].c || v[k].c + c2 + 1 < v[k + 1].c) continue;
			if (f(v[k].l - i, k - 1)) {
				for (int p = i; p <= j; p++)
					a[p] = v[k].c;
				int need = (v[k + 1].c - v[k].c - 1), len = nl - cr - 1;
				if (need) {
					int p = len / need, q = len%need, sum = 0;
					int cur = v[k].c + 1;
					for (int i = cr + 1; i < nl; i++) {
						a[i] = cur;
						sum++;
						if (sum == p) {
							if (q) q--;
							else {
								sum = 0; cur++;
							}
						}
						else if (sum > p) {
							sum = 0;
							cur++;
						}
					}
				}
				trace(v[k].l - i, k - 1);
			}
		}
	}
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		if (i == 1) {
			if (a[i] > 1) {
				puts("-1");
				return 0;
			}
			a[i] = 1;
		}
		if (a[i]) {
			if (!l[a[i]]) l[a[i]] = i;
			r[a[i]] = i;
		}
	}
	for (int i = 1; i <= 100000; i++) if (l[i]) {
		v.push_back(p(l[i], r[i], i));
	}
	if (v.size() == 1) {
		if (v[0].r - v[0].l + 1 > 5) {
			puts("-1");
			return 0;
		}
		if (v[0].r == 1) {
			a[2] = 1;
			v[0].r++;
		}
		for (int i = v[0].l; i <= v[0].r; i++)
			a[i] = 1;
		int rem = (n - v[0].r) / 2;
		int cur = 2;
		for (int i = v[0].r + 1; i < n; i += 2) {
			a[i] = a[i + 1] = cur;
			cur++;
		}
		if (a[n] == 0) a[n] = a[n - 1];
		printf("%d\n", a[n]);
		for (int i = 1; i <= n; i++)
			printf("%d ", a[i]);
		puts("");
		return 0;
	}
	sort(v.begin(), v.end(), cmp);
	for (int i = 0; i < v.size(); i++) {
		if (v[i].r - v[i].l + 1 > 5) {
			puts("-1");
			return 0;
		}
		if (i && v[i].l <= v[i - 1].r) {
			puts("-1");
			return 0;
		}
		if (i && v[i].c < v[i - 1].c) {
			puts("-1");
			return 0;
		}
		if (i > 0 && i < v.size() - 1 && v[i].l == v[i].r && v[i - 1].r == v[i].l - 1 && v[i + 1].l == v[i].r + 1) {
			puts("-1");
			return 0;
		}
	}
	for (int i = 0; i < v.size(); i++) {
		for (int j = v[i].l; j <= v[i].r; j++)
			a[j] = v[i].c;
	}
	memset(d, -1, sizeof(d));
	for (int l = v.back().l - 4; l <= v.back().l; l++) {
		for (int r = v.back().r; r <= v.back().r + 4; r++) {
			if (r - l + 1 > 5 || r - l + 1 < 2) continue;
			if (f(v.back().l - l, v.size() - 2)) {
				trace(v.back().l - l, v.size() - 2);
				for (int j = l; j <= r; j++)
					a[j] = v.back().c;
				int cur = v.back().c + 1;
				for (int j = r + 1; j < n; j += 2) {
					a[j] = a[j + 1] = cur;
					cur++;
				}
				if (a[n] == 0) a[n] = a[n - 1];
				printf("%d\n", a[n]);
				for (int j = 1; j <= n; j++)
					printf("%d ", a[j]);
				puts("");
				return 0;
			}
		}
	}
	puts("-1");
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>

using namespace std;

class p {
public:
	int l, r, c;
	p(int l, int r, int c)
		:l(l), r(r), c(c)
	{}
};

bool cmp(const p &i, const p &j) {
	return i.l < j.l;
}

int a[200002], n;
vector<p> v;
int l[100001], r[100001];
int d[6][100001];

int f(int h, int k) {
	if (k < 0) return 1;
	if (d[h][k] >= 0) return d[h][k];
	if (v[k + 1].l - h <= v[k].r) return d[h][k] = 0;
	for (int i = v[k].l - 4; i <= v[k].l; i++) {
		for (int j = v[k].r; j <= v[k].r + 4; j++) {
			int cl = i, cr = j, nl = v[k + 1].l - h;
			if (cl<1 || cr>n || cr >= nl) continue;
			if (cr - cl + 1 > 5 || cr - cl + 1 < 2) continue;
			int c1 = (nl - cr + 3) / 5, c2 = (nl - cr - 1) / 2;
			if (v[k].c + c1 + 1 > v[k + 1].c || v[k].c + c2 + 1 < v[k + 1].c) continue;
			if (f(v[k].l - i, k - 1)) return d[h][k] = 1;
		}
	}
	return d[h][k] = 0;
}

void trace(int h, int k) {
	if (k < 0) return;
	for (int i = v[k].l - 4; i <= v[k].l; i++) {
		for (int j = v[k].r; j <= v[k].r + 4; j++) {
			int cl = i, cr = j, nl = v[k + 1].l - h;
			if (cl<1 || cr>n || cr >= nl) continue;
			if (cr - cl + 1 > 5 || cr - cl + 1 < 2) continue;
			int c1 = (nl - cr + 3) / 5, c2 = (nl - cr - 1) / 2;
			if (v[k].c + c1 + 1 > v[k + 1].c || v[k].c + c2 + 1 < v[k + 1].c) continue;
			if (f(v[k].l - i, k - 1)) {
				for (int p = i; p <= j; p++)
					a[p] = v[k].c;
				int need = (v[k + 1].c - v[k].c - 1), len = nl - cr - 1;
				if (need) {
					int p = len / need, q = len%need, sum = 0;
					int cur = v[k].c + 1;
					for (int i = cr + 1; i < nl; i++) {
						a[i] = cur;
						sum++;
						if (sum == p) {
							if (q) q--;
							else {
								sum = 0; cur++;
							}
						}
						else if (sum > p) {
							sum = 0;
							cur++;
						}
					}
				}
				trace(v[k].l - i, k - 1);
				return;
			}
		}
	}
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		if (i == 1) {
			if (a[i] > 1) {
				puts("-1");
				return 0;
			}
			a[i] = 1;
		}
		if (a[i]) {
			if (!l[a[i]]) l[a[i]] = i;
			r[a[i]] = i;
		}
	}
	for (int i = 1; i <= 100000; i++) if (l[i]) {
		v.push_back(p(l[i], r[i], i));
	}
	if (v.size() == 1) {
		if (v[0].r - v[0].l + 1 > 5) {
			puts("-1");
			return 0;
		}
		if (v[0].r == 1) {
			a[2] = 1;
			v[0].r++;
		}
		for (int i = v[0].l; i <= v[0].r; i++)
			a[i] = 1;
		int rem = (n - v[0].r) / 2;
		int cur = 2;
		for (int i = v[0].r + 1; i < n; i += 2) {
			a[i] = a[i + 1] = cur;
			cur++;
		}
		if (a[n] == 0) a[n] = a[n - 1];
		printf("%d\n", a[n]);
		for (int i = 1; i <= n; i++)
			printf("%d ", a[i]);
		puts("");
		return 0;
	}
	sort(v.begin(), v.end(), cmp);
	for (int i = 0; i < v.size(); i++) {
		if (v[i].r - v[i].l + 1 > 5) {
			puts("-1");
			return 0;
		}
		if (i && v[i].l <= v[i - 1].r) {
			puts("-1");
			return 0;
		}
		if (i && v[i].c < v[i - 1].c) {
			puts("-1");
			return 0;
		}
		if (i > 0 && i < v.size() - 1 && v[i].l == v[i].r && v[i - 1].r == v[i].l - 1 && v[i + 1].l == v[i].r + 1) {
			puts("-1");
			return 0;
		}
	}
	for (int i = 0; i < v.size(); i++) {
		for (int j = v[i].l; j <= v[i].r; j++)
			a[j] = v[i].c;
	}
	memset(d, -1, sizeof(d));
	for (int l = v.back().l - 4; l <= v.back().l; l++) {
		for (int r = v.back().r; r <= v.back().r + 4; r++) {
			if (r - l + 1 > 5 || r - l + 1 < 2) continue;
			if (f(v.back().l - l, v.size() - 2)) {
				if (r == n - 1 && r - l + 1 == 5) continue;
				trace(v.back().l - l, v.size() - 2);
				for (int j = l; j <= r; j++)
					a[j] = v.back().c;
				int cur = v.back().c + 1;
				for (int j = r + 1; j < n; j += 2) {
					a[j] = a[j + 1] = cur;
					cur++;
				}
				if (a[n] == 0) a[n] = a[n - 1];
				printf("%d\n", a[n]);
				for (int j = 1; j <= n; j++)
					printf("%d ", a[j]);
				puts("");
				return 0;
			}
		}
	}
	puts("-1");
	return 0;
}