Problem:
Problem G
巡回勇者問題
あなたは JAG 王国の勇者である．鍛錬を積んでいるあなたの現在の所持金は
10
100
である．
JAG 王国には
1, 2, ..., N
で番号付けされた
N
個の街がある．また，街
i
と街
i+1
(1 ≤ i < N)
の間には道路があり，通行すると所持金が
C
i
変動する．つまり，
C
i
が正のときはあなたの所持金が
|C
i
|
だけ増加するが，そうでないときはあなたの所持金が
|C
i
|
だけ減少する．
N
個の街すべてでクエストが発生しているため，勇者であるあなたは冒険に出かけることにした．冒険は以下を満たすものでなければならない．
冒険では，
N
個すべての街で 1 度ずつクエストを行わなければならない．
クエストを行う街の順番を
(x
1
, x
2
, ..., x
N
)
と定めたとする (
i ≠ j
ならば
x
i
≠ x
j
)．あなたは街
x
1
を出発地としてクエストを順番に行う．街
x
k
から
x
k+1
(1 ≤ k < N)
へ移動するときは，
x
k
から
x
k+1
まで最短経路で移動しなければならない．ここで「最短経路」とは，通る道路の数が最も少ない経路を指す．
街
x
N
で最後のクエストを行うと，そこで冒険は終了となる．
冒険が終了した後の所持金をできるだけ多くするには，どのような順番でクエストを行えばよいだろうか？
Input
入力は複数のデータセットからなる．各データセットは次の形式で表される．
N
C
1
C
2
...
C
N-1
各データセットは
2
行からなる．最初の行には街の数を表す整数
N
(2 ≤ N ≤ 3,000)
がある．次の行には空白で区切られた
N-1
個の整数
C
1
, C
2
, ..., C
N-1
がある．
C
i
は街
i
と街
i+1
の間を通行したときに所持金がいくら変動するかを表す値である．ここで
C
1
から
C
N-1
はすべて
-10
9
以上
10
9
以下である．
入力の終わりはゼロひとつを含む行で示す．データセットは 50 個以内である．
Output
各テストケースに対する出力は 2 行からなる．1 行目では，冒険が終了した後の所持金と，冒険を行う前の所持金との差としてあり得る最大値を出力する．2 行目では，その所持金を達成できるような，クエストを行う街の順番
(x
1
, x
2
, ..., x
N
)
をスペース区切りで出力する．
答えとしてあり得るものが複数ある場合は，どれを出力しても正解となる．
Sample Input
4
2 3 4
4
2 -3 4
6
-1 -1 -1 -1 -1
6
-1 -1 100 -1 -1
0
Output for the Sample Input
21
2 4 1 3
9
2 1 4 3
-5
1 2 3 4 5 6
492
1 4 3 5 2 6

Submitted solutions:

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define _USE_MATH_DEFINES
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using H = pair<int, int>;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
#define all(a) (a).begin(), (a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define rng(i, s, n) for(int i = (s) ; i < (n) ; i++)
#define rep(i, n) rng(i, 0, (n))
#define mkp make_pair
#define vec vector
#define pb emplace_back
#define siz(a) int((a).size())
#define crdcomp(b) sort(all((b))); (b).erase(unique(all((b))), (b).end())
#define getidx(b, i) (lower_bound(all((b)), (i))-(b).begin())
#define cdf(n) for(int __quetimes=(n); __quetimes>0; __quetimes--)
#define readv(a, n) (a).resize((n), 0); rep(i, (n)) (a)[i]=read()
#define found(a, x) (a.find(x)!=a.end())
#define slp(x, y) min((x), (y)), max((x), (y))
constexpr ll mod = (ll)1e9 + 7;
constexpr ll Mod = 998244353;
constexpr ld EPS = 1e-10;
constexpr ll inf = (ll)3 * 1e18;
constexpr int Inf = 1e9 + 10;
constexpr int dx[] = { -1, 1, 0, 0 }, dy[] = { 0, 0, -1, 1 };
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<class T = ll>T read() { T u, k = scanf("%lld", &u); return u; }
template<typename T>bool ina(pair<T, T> t, T h, T w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
template<typename T>void fin(T x) { cout << x << endl; exit(0); }/*
#include<atcoder/all>
using namespace atcoder;//*/
//--------------------------------------------------------------
signed main() {
	int n;
	while (cin >> n && n) {
		vl c(n);
		rep(i, n - 1) scanf("%lld", &c[i]);
		vec<vec<vl>>dp(n + 1, vec<vl>(n + 10, vl(2, -inf)));
		dp[0][0][0] = 0;
		for (int i = 0;i < n;i++) rep(j, n + 5) {
			if (i && j == 0) {
				dp[i][j][0] = dp[i][j][1] = -inf;
				continue;
			}
			if (j % 2 == 1) {
				if (dp[i][j][1] > -inf) {
					for (int k = j - 2;k <= j + 2;k++) if (k >= 0) {
						chmax(dp[i + 1][k][1], dp[i][j][1] + c[i] * k);
					}
				}
			}
			else {
				if (dp[i][j][0] > -inf) {
					for (int k = j - 2;k <= j + 2;k++) if (k >= 0) {
						chmax(dp[i + 1][k][abs(k - j) % 2], dp[i][j][0] + c[i] * k);
					}
				}
				if (dp[i][j][1] > -inf) {
					for (int k = j - 2;k <= j + 2;k += 2) if (k >= 0) {
						chmax(dp[i + 1][k][1], dp[i][j][1] + c[i] * k);
					}
				}
			}
		}
		vi cnt(n + 1);
		int cur = 0, cur2 = 1;
		for (int i = n - 1;i > 0;i--) {
			for (int j = cur - 2;j <= cur + 2;j++)if (j >= 0) {
				if (j % 2 == 1) {
					if (dp[i][j][1] > -inf) {
						int k = cur;
						if (abs(k - cur) <= 2) {
							if (cur2 == 1 && dp[i + 1][k][1] == dp[i][j][1] + c[i] * k) {
								cur = j;
								cur2 = 1;
								goto loop;
							}
						}
					}
				}
				else {
					if (dp[i][j][0] > -inf) {
						int k = cur;
						if (abs(j - k) <= 2) {
							if (cur2 == abs(k - j) % 2 && dp[i + 1][k][cur2] == dp[i][j][0] + c[i] * k) {
								cur = j;
								cur2 = 0;
								goto loop;
							}
						}
					}
					if (dp[i][j][1] > -inf) {
						int k = cur;
						if (abs(k - j) <= 2 && abs(k - j) % 2 == 0) {
							if (cur == k && cur2 == 1 && dp[i + 1][k][1] == dp[i][j][1] + c[i] * k) {
								cur = j;
								cur2 = 1;
								goto loop;
							}
						}
					}
				}
			}
		loop:;
			cnt[i] = cur;
		}

		int st = -1;
		for (int i = 0;i < n;i++) {
			if (st < 0 && cnt[i] % 2 != cnt[i + 1] % 2) {
				if (cnt[i] % 2 == 1) st = i;
				else st = i + 1;
				break;
			}
		}

		int sum = 0;
		rep(i, n + 1) sum += cnt[i];
		vi pos;
		int prev = -1;
		vec<bool>used(n, false);
		rep(_, sum) {
			if (cnt[st - 1] == cnt[st] && prev == 1 || cnt[st - 1] < cnt[st]) {
				if (prev != 1) {
					pos.pb(st);
					prev = 1;
				}
				cnt[st]--;
				st++;
			}
			else if (cnt[st - 1] == cnt[st] && prev == 0 || cnt[st - 1] > cnt[st]) {
				if (prev != 0) {
					pos.pb(st);
					prev = 0;
				}
				cnt[st - 1]--;
				st--;
			}
		}
		pos.pb(st);
		if (siz(pos) < n) {
			rep(i, siz(pos)) used[pos[i]] = true;
			rep(i, n)if (!used[i]) {
				rep(j, siz(pos) - 1) {
					if (pos[j] < i && i < pos[j + 1] || pos[j + 1] < i && i < pos[j]) {
						pos.insert(pos.begin() + j + 1, i);
						break;
					}
				}
			}
		}
		cout << dp[n][0][1] << endl;
		rep(i, siz(pos)) printf("%lld%c", pos[i], " \n"[i == n - 1]);
	}
}
accuracy: 1
time_ms: 5820
memory_kb: 492596
final_rank: 10

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

// 数値型
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int,int>;
using Pll = pair<ll, ll>;
using Pli = pair<ll, int>;
using Pil = pair<int, ll>;

// vector関連
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
template<typename T>
using vc = vector<T>;
template<typename T>
using vvc = vector<vc<T>>;
template<typename T>
using vvvc = vector<vvc<T>>;
template<typename T>
using vvvvc = vector<vvvc<T>>;

// priority_queue
template<typename T>
using pq = priority_queue<T>;
template<typename T>
using pqg = priority_queue<T, vc<T>, greater<T>>;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for(int i = a; i < (int)(b); i++)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define MIN(vec) *min_element(vec)
#define MAX(vec) *max_element(vec)
#define next_perm(vec) (vec).begin(), (vec).end()
#define UNIQUE(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define el "\n"
#define Yes cout << "Yes" << el
#define No cout << "No" << el
#define YES cout << "YES" << el
#define NO cout << "NO" << el
#define EPS 1e-8
#define Equal(a, b) (fabs((a)-(b)) < EPS) 
#define dbg(x) cerr << #x << "=" << x << el 

// 定数
const string abc = "abcdefghijklmnopqrstuvwxyz";
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
constexpr int INF = 1001001001;
constexpr ll LINF = 1001001001001001001ll;
constexpr int DX[] = {1, 0, -1, 0};
constexpr int DY[] = {0, 1, 0, -1};
constexpr int DX8[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int DY8[] = {0, 1, 0, -1, 1, -1, 1, -1};

template<typename T1, typename T2>
ostream &operator<< (ostream &os, pair<T1, T2> p) {
    os << "{" << p.first << "," << p.second << "}";
    return os;
}
template<typename T>
ostream &operator<< (ostream &os, vc<T> &vec) {
    int sz = vec.size();
    rep(i, sz){
        os << vec[i] << (i==sz-1?"":" ");
    }
    return os;
}

template<typename T1, typename T2>
istream &operator>> (istream &is, pair<T1, T2> &p) {
    is >> p.first >> p.second;
    return is;
}
template<typename T>
istream &operator>> (istream &is, vc<T> &vec) {
    int sz = vec.size();
    rep(i, sz) { is >> vec[i]; }
    return is;
}
/// @brief aとｂの最大値をaに格納。更新があったかbool値を返す
/// @tparam T1 
/// @tparam T2 
/// @param a 
/// @param b 
/// @return bool
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b){
    bool ret = a<b;
    if(ret) a = b;
    return ret;
}

/// @brief aとｂの最小値をaに格納。更新があったかbool値を返す
/// @tparam T1 
/// @tparam T2 
/// @param a 
/// @param b 
/// @return bool
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b){
    bool ret = a>b;
    if(ret) {a = b;}
    return ret;
}

inline void YesNo(bool flag){
    if(flag) {Yes;}
    else {No;}
    return;
}

inline void YESNO(bool flag){
    if(flag) {YES;}
    else {NO;}
    return;
}

inline bool outof(ll x, ll xlim){
    return (x<0 || x>=xlim);
}

template<typename T>
inline T sqnorm(T x, T y){
    return x*x+y*y;
}

/// @brief char->int
/// @param c 
/// @return int
inline int ctoi(char c){
    return c-'0';
}

/// @brief xを素因数分解
/// @param x 
/// @return vector<Pli>, 素因数の昇順に {p, cnt}
vector<Pli> prime_fact(ll x){
    vector<Pli> ret;
    for(ll i=2; i*i<=x; i++){
        if(x%i == 0){
            ret.emplace_back(i, 0);
            while(x%i == 0){
                ret.back().second++;
                x /= i;
            }
        }
    }
    if(x != 1) ret.emplace_back(x, 1);
    return ret;
}

/// @brief xの約数列挙
/// @param x 
/// @return vll, 約数の昇順
vll divisor_enum(ll x){
    vector<ll> ret;
    for(ll i=1; i*i<=x; i++){
        if(x%i == 0){
            ret.push_back(x/i);
            ret.push_back(i);
        }
    }
    sort(all(ret));
    UNIQUE(ret);
    return ret;
}

/// @brief 繰り返し二乗法。
/// @tparam T 
/// @param x 
/// @param k 
/// @param op 
/// @param e 
/// @return 
template<typename T>
T pow_t(T x, ll k, T (*op)(T, T), T (*e)()){
    T ret = e();
    while(k){
        if(k&1) ret *= x;
        x *= x;
        k >>= 1;
    }
    return ret;
}

ll powll(ll x, ll k){
    return pow_t<ll>(x, k, [](ll a, ll b) -> ll{return a*b;}, []() -> ll{return 1;});
}

inline int pop_cnt(ll x) { return __builtin_popcountll(x); }
inline int top_bit(ll x) { return (x==0?-1:63-__builtin_clzll(x));}

void main2();

int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    main2();
}


bool solve(pair<int, vll> input={-1, {}}){
    int n;
    if(input.first == -1) cin >> n;
    else n = input.first;
    if(n == 0) return false;
    vll c(n-1);
    if(input.first == -1) cin >> c;
    else c = input.second;
    c.push_back(0);
    vvvll dp(n+1, vvll(n+2, vll(3, -LINF)));
    dp[0][0][0] = 0;
    rep(i, n){
        rep(j, n){
            rep(k, 3){
                // +2
                chmax(dp[i+1][j+2][k], dp[i][j][k] + (j+2)*c[i]);
                // -2
                if(j-2 >= 0 + (i+1 != n)) chmax(dp[i+1][j-2][k], dp[i][j][k] + (j-2)*c[i]);
                // 0
                if(j >= 0 + (i+1 != n)) chmax(dp[i+1][j][k], dp[i][j][k] + j*c[i]);
                if(k == 2) break;
                // +1
                chmax(dp[i+1][j+1][k+1], dp[i][j][k] + (j+1)*c[i]);
                // -1
                if(j-1 >= 0 + (i+1 != n)) chmax(dp[i+1][j-1][k+1], dp[i][j][k] + (j-1)*c[i]);
            }
        }
    }
    vc<P> pm;
    {
        int now = 0, one = 2;
        for(int i=n-1; i>=0; i--){
            for(int j=-2; j<=2; j++){
                if(now-j <= 0 - (i == 0)) continue;
                if(abs(j) == 1 && one == 0) continue;
                if(dp[i][now-j][one-(abs(j)&1)] + c[i]*now == dp[i+1][now][one]){
                    pm.push_back({i, j});
                    now = now-j;
                    one -= abs(j)&1;
                    break;
                }
            }
        }
        assert(now == 0 && one == 0);
        reverse(all(pm));
        ll score = 0;
        rep(i, n){
            now += pm[i].second;
            score += now*c[i];
        }
        assert(score == dp[n][0][2]);
    }
    int id = -1;
    rep(i, n){
        if(abs(pm[i].second) == 1) id = i;
    }
    assert(id != -1);
    int dx = pm[id].second;
    vector<int> ord;
    ord.push_back(pm[id].first);
    pm.erase(pm.begin() + id);
    if(dx < 0) id--;
    while(pm.size()){
        if((abs(pm[id].second) == 1 && pm.size() != 1)){
            id += dx;
            continue;
        }
        if(pm[id].second == 0){
            ord.push_back(pm[id].first);
            pm.erase(pm.begin()+id);
            if(dx < 0) id--;
            continue;
        }
        if(pm[id].second*dx < 0){
            ord.push_back(pm[id].first);
            pm.erase(pm.begin()+id);
            dx *= -1;
            if(dx < 0) id--;
        }
        else{
            id += dx;
        }
    }
    {
        ll score = 0;
        rep(i, n-1){
            int dx = (ord[i] < ord[i+1] ? 1 : -1);
            for(int j=ord[i]; j != ord[i+1]; j += dx){
                score += (dx > 0 ? c[j] : c[j-1]);
                // cout << (dx > 0 ? c[j] : c[j-1]) << " " << score << endl;
            }
        }
        assert(score == dp[n][0][2]);
    }
    assert(ord.size() == n);
    cout << dp[n][0][2] << endl;
    for(int i=0; i<n; i++){
        cout << ord[i]+1 <<  (i == n-1 ? "" : " ");
    }
    cout << endl;

    return true;
}

random_device seed_gen;
mt19937_64 rnd(seed_gen());
uniform_int_distribution<int> uid(-5, 5);
pair<int, vll> generater(int n){
    pair<int, vll> ret;
    ret.first = n;
    rep(i, n-1){
        ret.second.push_back(uid(rnd));
    }
    return ret;
}
void main2(){
    while(true){
        // auto gen = generater(6);
        // cout << gen << endl;
        // pair<int, vll> gen = {6, {-4, 5, -4, 5, 3}};
        if(solve()) continue;
        break;
    }
}
accuracy: 1
time_ms: 6060
memory_kb: 491200
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)
#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)
#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)
#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)
#define each(p,a) for(auto &p:a)
typedef vector<ll> vll;
typedef vector<pair<ll ,ll>> vpll;
typedef vector<vector<ll>> vvll;
typedef set<ll> sll;
typedef map<ll , ll> mpll;
typedef pair<ll ,ll> pll;
typedef tuple<ll , ll , ll> tpl3;
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)
#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)
#define all(a)  (a).begin(),(a).end()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define sz(x) (ll)x.size()
// << std::fixed << std::setprecision(10)
const ll INF = 1LL << 60;
const ld EPS = 1e-9;
 
ll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}
ll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}
inline ll positive_mod(ll a,ll m){return (a % m + m)%m;}
inline ll popcnt(ull a){ return __builtin_popcountll(a);}
//0indexed
inline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}
inline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}
template<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}
template<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}
template<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}
template<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?" ":"");}return os;}
template<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << "{" << p.first << "," << p.second << "}";return os;}
template<class... T>void input(T&... a){(cin >> ... >> a);}
void print(){cout << endl;}
template<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}
template<class T> void pspace(const T& a){ cout << a << ' ';}
void perr(){cerr << endl;}
template<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}
void yes(bool i = true){ return print(i?"yes":"no"); }
void Yes(bool i = true){ return print(i?"Yes":"No"); }
void YES(bool i = true){ return print(i?"YES":"NO"); }
template <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}
template <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}
template <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}
template <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}
//grid探索用
vector<ll> _ta = {0,0,1,-1,1,1,-1,-1};
vector<ll> _yo = {1,-1,0,0,1,-1,1,-1};
bool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}
  
ll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}
ll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} 
const ll MOD9 = 998244353LL;
const ll MOD10 = 1000000007LL;

pair<ll,vll> solve(ll n,vll a){
  vector dp(n,vector<unordered_map<ll,ll>>(3));
  auto add = [&](ll i,ll j,ll k,ll val){
    if(dp[i][j].contains(k)){
      chmax(dp[i][j][k],val);
    }else dp[i][j][k] = val;
  };
  dp[0][0][0] = 0;
  rep(i,n-1){
    rep(j,3){
      for(auto &[num,val]:dp[i][j]){
				if(num-2*(n-i) > 0)continue;
        //普通
        add(i+1,j,num+2,val + a[i] * (num+2));
        if(num  != 0){
          add(i+1,j,num,val + a[i] * num);
        }
        if(num-2 > 0){
          add(i+1,j,num-2,val + a[i] *(num-2));
        }
        //繰り上がる
        if(j+1 <= 2){
          add(i+1,j+1,num+1,val + a[i] *(num+1));
          if(num -1 > 0){
            add(i+1,j+1,num-1,val + a[i] *(num-1));
          }
        }
      }
    }
  }
  // cout <<max(dp[n-1][2][2],dp[n-1][1][1]) << endl;
  //復元

  ll ansv = -INF;
  
  
  vll cnt;
  {
    ll i = n-1,j =2,k=2,val = -INF;
    if(dp[n-1][2].contains(2)){
      if(chmax(ansv,dp[n-1][2][2])){
        val = ansv;
      }
    }
    if(dp[n-1][1].contains(1)){
      if(chmax(ansv,dp[n-1][1][1])){
        val = ansv;
        i = n-1,j = 1,k= 1;
      }
      
    }
    
    auto check = [&](ll i,ll j,ll k,ll val){
      if(!dp[i][j].contains(k))return false;
      else return dp[i][j][k] == val;
    };
    while(i > 0){
      cnt.push_back(k);
      //j同じでの遷移
      val -= a[i-1]*k;
      if(check(i-1,j,k-2,val)){
        k = k-2;
      }else if(check(i-1,j,k,val)){
      }else if(check(i-1,j,k+2,val)){
        k = k+2;
      }else {
        assert(j > 0);
        if(check(i-1,j-1,k+1,val)){
          k = k+1;
          j--;
        }else if(check(i-1,j-1,k-1,val)){
          k = k-1;
          j--;
        }
      }
      i--;
    }
  }

  reverse(all(cnt));
  // cout << cnt << endl;
  vll plc;
  rep(i,sz(cnt)-1){
    if(abs(cnt[i]-cnt[i+1]) == 1){
      plc.push_back(i+1);
    }
  }
  if(cnt.front() == 1){
    plc.push_back(0);
  }
  if(cnt.back() == 1){
    plc.push_back(n-1);
  }
  assert(sz(plc) ==2);

  vll ans;

  vector<ll> found(n,0);//n頂点
  ll fcnt = 0;
  auto dfs = [&](auto dfs, ll v)->void{
    found[v] = 1;
    ans.push_back(v);
    fcnt++;
    if(fcnt == n-1)return ;
    //gでグラフを受け取っている
    bool plus = true;
    if(v == 0){
      plus = true;
    }else if(v== n-1){
      plus = false;
    }else{
      plus = cnt[v] > cnt[v-1];
    }
    v += (plus ? 1: -1); 
    cnt[v-(plus?1: 0)]--;
    // if(cnt[v-(plus?1: 0)]==-1)return ;
    while(v!= 0 && v != n-1 && (found[v] == 1 or  (abs(cnt[v]-cnt[v-1]) != 1))){
      v += (plus ? 1: -1);
      cnt[v-(plus?1: 0)]--;
    }
    dfs(dfs,v);
  };
  dfs(dfs,plc[0]);
  ans.push_back(plc[1]);
  ans++;
  return {ansv,ans};
}

//参照渡しで値を作成して渡す
void Maketest(ll &n,vll &c){
//配列を渡すときは最初に.clear()をしておく
  random_device seed_gen;
  mt19937_64 rnd(seed_gen());
  
  uniform_int_distribution<ll> da(2, 4);
  uniform_int_distribution<ll> db(-5,5);
  n = da(rnd);
  rep(i,n-1){
    c.push_back(db(rnd));
  }

  uniform_int_distribution<ll> dc(0, 1);
  uniform_int_distribution<ll> dd(0, 1);
}

void myans(){
  //答えの型注意
}

void correct(){
  //答えの型注意
}

void test(){
  rep(z,1000){
    ll n;
    vll c;
    Maketest(n,c);
    cout << n << endl;
    cout << c << endl;
    cout << endl;
    auto ret = solve(n,c);
    vll ord = ret.second;
    sort(all(ord));
    UNIQUE(ord);
    if(sz(ord) != n){
      cout << "wrong siz" << endl;
      cout << n << endl;
      cout << c << endl;
      cout << ret.first << endl;
      cout << ret.second << endl;
      cout <<  endl;
      return ;
      continue;
    }
    ll sum = 0;
    rep(i,n-1){
      ll s = ret.second[i];
      s--;

      ll g = ret.second[i+1];
      g--;
      while(s < g){
        sum += c[s++];
      }
      while(s > g){
        sum += c[--s];
      }
    }
    if(sum != ret.first){
      cout << "sum = " << sum << endl;
      cout << n << endl;
      cout << c << endl;
      cout << ret.first << endl;
      cout << ret.second << endl;
      cout <<  endl;
      return ;
    }
  }
}

int main(){
  ios::sync_with_stdio(false);cin.tie(nullptr);
  while(true){
    LL(n);
    if(n == 0)break;
    vll a(n-1);
    cin >> a;
    auto ret = solve(n,a);
    cout << ret.first << endl;
    cout << ret.second << endl;
  }
  // test();
}
accuracy: 1
time_ms: 6030
memory_kb: 295064
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0; i<ll(n); i++)
template<class A, class B> void chmax(A& a, const B& b){ if(a < b) a = b; }


bool testcase(){
    ll N; cin >> N;
    if(N == 0) return false;
    vector<ll> A(N,0); rep(i,N-1) cin >> A[i];

    auto dp = vector(N+1, vector(4, vector<pair<ll, pair<ll, ll>>>(N, {-(1ll<<61), {-1,-1}})));

    dp[0][0][0].first = 0;
    rep(s,N){
        rep(f,3){
            rep(a,N){
                for(ll b=a-2; b<=a+2; b++) if((1 <= b || (s == N-1 && b == 0)) && b < N){
                    ll g = (b%2 != f%2 ? f+1 : f);
                    chmax(dp[s+1][g][b], make_pair(dp[s][f][a].first + b * A[s], make_pair(f,a)));
                }
            }
        }
    }

    vector<ll> ph(N+1); ph[N] = 2;
    vector<ll> cnt(N+1);
    for(ll s=N-1; s>=0; s--){
        auto pre = dp[s+1][ph[s+1]][cnt[s+1]].second;
        ph[s] = pre.first;
        cnt[s] = pre.second;
    }

    ll s = 0;
    rep(i,N) if(ph[i] == 0 && ph[i+1] == 1) s = i+1;
    ll g = 0;
    rep(i,N) if(ph[i] == 1 && ph[i+1] == 2) g = i+1;

    ll dx = 1;
    if(cnt[s-1] > cnt[s]) dx = -1;
    
    set<ll> LX, RX;
    rep(i,N) if(cnt[i] + 2 == cnt[i+1]) LX.insert(i+1);
    rep(i,N) if(cnt[i] == cnt[i+1] + 2) RX.insert(i+1);
    auto leftLX = [&](ll p) -> ll {
        auto i = LX.upper_bound(p);
        if(i == LX.begin()) return -1;
        i--;
        return *i;
    };
    auto rightRX = [&](ll p) -> ll {
        auto i = RX.lower_bound(p);
        if(i == RX.end()) return -1;
        return *i;
    };

    //cout << "s = " << s << " , g = " << g << endl;

    vector<ll> ans;
    ans.push_back(s);
    while(true){
        if(dx == -1){
            ll p = leftLX(ans.back());
            if(p == -1) break;
            LX.erase(p);
            ans.push_back(p);
            dx = 1;
        } else {
            ll p = rightRX(ans.back());
            if(p == -1) break;
            RX.erase(p);
            ans.push_back(p);
            dx = -1;
        }
    }
    ans.push_back(g);
    
    vector<ll> used(N+1);
    for(ll i : ans) used[i] = 1;
    for(ll i=1; i<=N; i++) if(used[i] == 0){
        rep(j,ans.size()-1) if(min(ans[j], ans[j+1]) <= i && i <= max(ans[j], ans[j+1])){
            ans.insert(ans.begin() + j + 1, i);
            used[i] = 1;
            break;
        }
    }

    cout << dp[N][2][0].first << "\n";
    //rep(i,N+1) cout << cnt[i] << " ";
    //cout << endl;
    rep(i,ans.size()){
        if(i) cout << " ";
        cout << ans[i];
    }
    cout << "\n";;

    return true;
}

int main(){
    while(testcase());
    return 0;
}
accuracy: 1
time_ms: 6970
memory_kb: 839588
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0; i<ll(n); i++)
template<class A, class B> void chmax(A& a, const B& b){ if(a < b) a = b; }


bool testcase(){
    ll N; cin >> N;
    if(N == 0) return false;
    vector<ll> A(N,0); rep(i,N-1) cin >> A[i];

    auto dp = vector(N+1, vector(4, vector<pair<ll, pair<ll, ll>>>(N, {-(1ll<<61), {-1,-1}})));

    dp[0][0][0].first = 0;
    rep(s,N){
        rep(f,3){
            rep(a,N){
                for(ll b=a-2; b<=a+2; b++) if((1 <= b || (s == N-1 && b == 0)) && b < N){
                    ll g = (b%2 != f%2 ? f+1 : f);
                    chmax(dp[s+1][g][b], make_pair(dp[s][f][a].first + b * A[s], make_pair(f,a)));
                }
            }
        }
    }

    vector<ll> ph(N+1); ph[N] = 2;
    vector<ll> cnt(N+1);
    for(ll s=N-1; s>=0; s--){
        auto pre = dp[s+1][ph[s+1]][cnt[s+1]].second;
        ph[s] = pre.first;
        cnt[s] = pre.second;
    }

    ll s = 0;
    rep(i,N) if(ph[i] == 0 && ph[i+1] == 1) s = i+1;
    ll g = 0;
    rep(i,N) if(ph[i] == 1 && ph[i+1] == 2) g = i+1;

    ll dx = 1;
    if(cnt[s-1] > cnt[s]) dx = -1;
    
    set<ll> LX, RX;
    rep(i,N) if(cnt[i] + 2 == cnt[i+1]) LX.insert(i+1);
    rep(i,N) if(cnt[i] == cnt[i+1] + 2) RX.insert(i+1);
    auto leftLX = [&](ll p) -> ll {
        auto i = LX.upper_bound(p);
        if(i == LX.begin()) return -1;
        i--;
        return *i;
    };
    auto rightRX = [&](ll p) -> ll {
        auto i = RX.lower_bound(p);
        if(i == RX.end()) return -1;
        return *i;
    };

    //cout << "s = " << s << " , g = " << g << endl;

    vector<ll> ans;
    ans.push_back(s);
    while(true){
        if(dx == -1){
            ll p = leftLX(ans.back());
            if(p == -1) break;
            LX.erase(p);
            ans.push_back(p);
            dx = 1;
        } else {
            ll p = rightRX(ans.back());
            if(p == -1) break;
            RX.erase(p);
            ans.push_back(p);
            dx = -1;
        }
    }
    ans.push_back(g);
    
    vector<ll> used(N+1);
    for(ll i : ans) used[i] = 1;
    for(ll i=1; i<=N; i++) if(used[i] == 0){
        rep(j,ans.size()-1) if(min(ans[j], ans[j+1]) <= i && i <= max(ans[j], ans[j+1])){
            ans.insert(ans.begin() + j + 1, i);
            used[i] = 1;
            break;
        }
    }

    cout << dp[N][2][0].first << "\n";
    //rep(i,N+1) cout << cnt[i] << " ";
    //cout << endl;
    rep(i,ans.size()){
        if(i) cout << " ";
        cout << ans[i];
    }
    cout << "\n";;

    return true;
}

int main(){
    while(testcase());
    return 0;
}
accuracy: 1
time_ms: 4700
memory_kb: 839836
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll LINF = 4321001001001001001;
template <typename T>
bool chmin(T &a, const T &b) {
	if (b < a) {
		a = b;
		return true;
	}
	return false;
}
template <typename T>
bool chmax(T &a, const T &b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
#ifdef _DEBUG
#define show(x)          \
	cerr << #x << " : "; \
	showVal(x)
template <typename T>
void showVal(const T &a) {
	cerr << a << endl;
}
template <typename T, typename U>
void showVal(const pair<T, U> &a) {
	cerr << a.first << " " << a.second << endl;
}
template <typename T>
void showVal(const vector<T> &a) {
	for (const T &v : a) cerr << v << " ";
	cerr << endl;
}
template <typename T, typename U>
void showVal(const vector<pair<T, U>> &a) {
	cerr << endl;
	for (const pair<T, U> &v : a) cerr << v.first << " " << v.second << endl;
}
template <typename T, typename U>
void showVal(const map<T, U> &a) {
	cerr << endl;
	for (const auto &v : a) cerr << "[" << v.first << "] " << v.second << endl;
}
template <typename T>
void showVal(const vector<vector<T>> &a) {
	cerr << endl;
	for (const vector<T> &v : a) showVal(v);
}
#else
#define show(x)
#endif
vector<vector<vector<tuple<int, int, int>>>> from(3001, vector<vector<tuple<int, int, int>>>(3001, vector<tuple<int, int, int>>(3, {-1, -1, -1})));
int main() {
	while (1) {
		int n;
		cin >> n;
		if (n == 0) break;
		vector<ll> c(n);
		for (int i = 0; i < n - 1; i++) {
			cin >> c[i];
		}
		vector<vector<ll>> dp(n + 1, vector<ll>(3, -LINF));
		c[n - 1] = 0;
		dp[0][0] = 0;
		from[0][0][0] = {-1, -1, -1};
		for (int i = 0; i < n; i++) {
			vector<vector<ll>> ndp(n + 1, vector<ll>(3, -LINF));
			for (int j = 0; j <= n; j++) {
				for (int k = 0; k < 3; k++) {
					auto upd = [&](int d, int dk) {
						if (i != n - 1 && j + d == 0) return;
						if (j + d <= n && j + d >= 0 && k + dk < 3) {
							if (chmax(ndp[j + d][k + dk], dp[j][k] + c[i] * (j + d))) {
								from[i + 1][j + d][k + dk] = {i, j, k};
							}
						}
					};
					upd(0, 0);
					upd(2, 0);
					upd(-2, 0);
					// if (i != n - 1) {
					upd(1, 1);
					upd(-1, 1);
				}
				// }
			}
			swap(ndp, dp);
		}
		// show(dp);
		ll ans = dp[0][2];
		cout << ans << endl;
		int ni = n, nj = 0, nk = 2;
		vector<int> s = {0};
		while (1) {
			auto [a, b, c] = from[ni][nj][nk];
			if (a == -1) break;
			ni = a;
			nj = b;
			nk = c;
			s.push_back(nj);
		}
		reverse(s.begin(), s.end());
		assert(s.size() == n + 1);
		vector<int> a(n);
		for (int i = 0; i < n; i++) {
			a[i] = s[i + 1] - s[i];
		}
		show(s);
		show(a);
		int sti = -1, gli = -1;
		for (int i = 0; i < n; i++) {
			if (abs(a[i]) == +1) {
				if (sti == -1) {
					sti = i;
				} else {
					gli = i;
					break;
				}
			}
		}
		assert(sti != -1);
		assert(gli != -1);
		int nowi = sti;
		vector<int> ord = {nowi};
		vector<bool> isvis(n, false);
		isvis[nowi] = true;
		int dir = a[nowi] == 1 ? 1 : -1;
		while (1) {
			int nexi = -1;
			for (int i = nowi + dir; i >= 0 && i < n; i += dir) {
				if (isvis[i]) continue;
				if (a[i] == dir * (-2)) {
					nexi = i;
					break;
				}
				if (a[i] == 0) {
					ord.push_back(i);
					isvis[i] = true;
				}
			}
			if (nexi == -1) break;
			assert(nexi != -1);
			isvis[nexi] = true;
			ord.push_back(nexi);
			dir *= -1;
			nowi = nexi;
		}
		for (int i = nowi + dir; i >= 0 && i < n; i += dir) {
			if (isvis[i]) continue;
			if (a[i] == 0) {
				ord.push_back(i);
				isvis[i] = true;
			}
			if (i == gli) {
				ord.push_back(gli);
				isvis[gli] = true;
				break;
			}
		}
		show(isvis);
		for (int i = 0; i < n; i++) {
			cout << ord[i] + 1;
			if (i == n - 1)
				cout << endl;
			else
				cout << " ";
		}
	}
	return 0;
}
accuracy: 1
time_ms: 5640
memory_kb: 637056
final_rank: 15

code_snippet: #include <bits/stdc++.h>

using namespace std;
using ll = long long;

bool solve() {
    int n; cin >> n;
    if(n == 0) return false;
    vector<ll> a(n-1);
    for(int i = 0; i < n-1; i++) cin >> a[i];
    a.push_back(0);
    ll INF = 1LL << 60;
    vector dp(3, vector(n+1, vector<ll>(n, -INF)));
    vector prev(3, vector(n+1, vector<tuple<int, int, int>>(n)));
    dp[0][0][0] = 0;
    for(int i = 0; i < n; i++) {
        for(int t = 0; t <= 2; t++) {
            for(int j = 0; j < n; j++) {
                for(int dj : {1, -1}) {
                    int nj = j+dj;
                    ll nval = dp[t][i][j] + a[i]*nj;
                    if(t+1 <= 2 && nj >= 0 && nj < n && (i+1 == n || nj > 0)) {
                        if(dp[t+1][i+1][j+dj] < nval) {
                            dp[t+1][i+1][j+dj] = nval;
                            prev[t+1][i+1][j+dj] = {t, i, j};
                        }
                    }
                }
                for(int dj : {2, 0, -2}) {
                    int nj = j+dj;
                    ll nval = dp[t][i][j] + a[i]*nj;
                    if(nj >= 0 && nj < n && (i+1 == n || nj > 0)) {
                        if(dp[t][i+1][j+dj] < nval) {
                            dp[t][i+1][j+dj] = nval;
                            prev[t][i+1][j+dj] = {t, i, j};
                        }
                    }
                }
            }
        }
    }
    cout << dp[2][n][0] << '\n';
    vector<int> diff(n);
    int t = 2, i = n, j = 0;
    while(i > 0) {
        auto [nt, ni, nj] = prev[t][i][j];
        diff[ni] = j-nj;
        t = nt, i = ni, j = nj;
    }
    int s = -1;
    for(int i = 0; i < n; i++) {
        if(diff[i] == 1 || diff[i] == -1) s = i;
    }
    vector<int> seen(n);
    vector<int> ans;
    seen[s] = 1;
    ans.push_back(s);
    while(1) {
        if(diff[s] > 0) {
            int t = -1;
            int ns = -1;
            for(int i = s+1; i < n; i++) {
                if(!seen[i] && diff[i] == -1) {
                    t = i;
                }
                if(!seen[i] && diff[i] == -2) {
                    ns = i;
                    break;
                }
            }
            if(ns == -1) {
                assert(t != -1);
                ns = t;
            }
            for(int i = s+1; i < ns; i++) {
                if(!seen[i] && diff[i] == 0) {
                    seen[i] = 1;
                    ans.push_back(i);
                }
            }
            seen[ns] = 1;
            ans.push_back(ns);
            s = ns;
            if(s == t) break;
        }
        if(diff[s] < 0) {
            int t = -1, ns = -1;
            for(int i = s-1; i >= 0; i--) {
                if(!seen[i] && diff[i] == 1) {
                    t = i;
                }
                if(!seen[i] && diff[i] == 2) {
                    ns = i;
                    break;
                }
            }
            if(ns == -1) {
                assert(t != -1);
                ns = t;
            }
            for(int i = s-1; i > ns; i--) {
                if(!seen[i] && diff[i] == 0) {
                    seen[i] = 1;
                    ans.push_back(i);
                }
            }
            seen[ns] = 1;
            ans.push_back(ns);
            s = ns;
            if(s == t) break;
        }
    }
    assert(ssize(ans) == n);
    for(int i = 0; i < n; i++) {
        cout << ans[i]+1 << (i==n-1 ? '\n' : ' ');
    }
    return true;
}

int main() {
    while(solve());
}
accuracy: 1
time_ms: 4700
memory_kb: 631024
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using LL = long long;

const LL INF = 1LL << 60;

int main(){
	int n;
	while(cin >> n && n){
		vector<LL> cs(n);
		for(int i = 0; i < n - 1; ++i){
			cin >> cs[i];
		}
		const int lim = n + 6;
		vector<vector<vector<LL>>> dp(n + 1);
		vector<vector<vector<int>>> delta(n + 1);
		for(int i = 0; i < n + 1; ++i){
			dp[i].assign(3, vector<LL>(lim / 2 + 1, -INF));
			delta[i].assign(3, vector<int>(lim / 2 + 1));
		}
		dp[0][0][0] = 0;
		for(int i = 0; i < n; ++i){
			LL c = cs[i];
			for(int u = 0; u <= 2; ++u){
				for(int a = u & 1; a < lim; a += 2){
					if(dp[i][u][a >> 1] == -INF){ continue; }
					for(int d = -2; d <= 2; ++d){
						int v = u + (d & 1);
						if(v > 2){ continue; }
						int b = a + d;
						if(b < 0 || b >= lim){ continue; }
						if((b == 0) != (i == n - 1)){ continue; }
						LL t = dp[i][u][a >> 1] + c * b;
						if(dp[i + 1][v][b >> 1] < t){
							dp[i + 1][v][b >> 1] = t;
							delta[i + 1][v][b >> 1] = d;
						}
					}
				}
			}
		}
		
		LL ans1 = dp[n][2][0];
		
		vector<int> rem(n + 1);
		int u = 2, a = 0;
		for(int j = n; j > 0; --j){
			int d = delta[j][u][a >> 1];
			rem[j] = a;
			a -= d;
			u -= d & 1;
		}
		
		int p;
		for(p = 1; !((rem[p - 1] ^ rem[p]) & 1); ++p);
		vector<int> used(n + 1);
		used[p] = 1;
		vector<int> ord;
		int prvdir = 0;
		while(rem[p - 1] || rem[p]){
			int dir;
			if(rem[p - 1] < rem[p]){ dir = 1; }
			else if(rem[p - 1] > rem[p]){ dir = -1; }
			else{ dir = prvdir; }
			if(dir != prvdir){
				ord.push_back(p);
				used[p] = 1;
			}
			if(dir == 1){
				--rem[p];
				++p;
			}
			else{
				--p;
				--rem[p];
			}
			prvdir = dir;
		}
		ord.push_back(p);
		used[p] = 1;

		vector<int> ans2 = {ord[0]};
		for(int i = 1; i < (int)ord.size(); ++i){
			int x = ord[i - 1], y = ord[i];
			int d = x < y ? 1 : -1;
			for(int j = x + d; j != y; j += d){
				if(!used[j]){
					used[j] = 1;
					ans2.push_back(j);
				}
			}
			ans2.push_back(y);
		}
		
		char spr = '\n';
		cout << ans1;
		for(int x : ans2){
			cout << spr << x;
			spr = ' ';
		}
		cout << endl;
	}
}
accuracy: 1
time_ms: 2360
memory_kb: 178020
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using LL = long long;

const LL INF = 1LL << 60;

int main(){
	int n;
	while(cin >> n && n){
		vector<LL> cs(n);
		for(int i = 0; i < n - 1; ++i){
			cin >> cs[i];
		}
		const int lim = n + 6;
		vector<vector<vector<LL>>> dp(n + 1);
		vector<vector<vector<int>>> delta(n + 1);
		for(int i = 0; i < n + 1; ++i){
			dp[i].assign(3, vector<LL>(lim / 2 + 1, -INF));
			delta[i].assign(3, vector<int>(lim / 2 + 1));
		}
		dp[0][0][0] = 0;
		for(int i = 0; i < n; ++i){
			LL c = cs[i];
			for(int u = 0; u <= 2; ++u){
				for(int a = u & 1; a < lim; a += 2){
					if(dp[i][u][a >> 1] == -INF){ continue; }
					for(int d = -2; d <= 2; ++d){
						int v = u + (d & 1);
						if(v > 2){ continue; }
						int b = a + d;
						if(b < 0 || b >= lim){ continue; }
						if((b == 0) != (i == n - 1)){ continue; }
						LL t = dp[i][u][a >> 1] + c * b;
						if(dp[i + 1][v][b >> 1] < t){
							dp[i + 1][v][b >> 1] = t;
							delta[i + 1][v][b >> 1] = d;
						}
					}
				}
			}
		}
		
		LL ans1 = dp[n][2][0];
		
		vector<int> rem(n + 1);
		int u = 2, a = 0;
		for(int j = n; j > 0; --j){
			int d = delta[j][u][a >> 1];
			rem[j] = a;
			a -= d;
			u -= d & 1;
		}
		
		int p;
		for(p = 1; !((rem[p - 1] ^ rem[p]) & 1); ++p);
		vector<int> used(n + 1);
		used[p] = 1;
		vector<int> ord;
		int prvdir = 0;
		while(rem[p - 1] || rem[p]){
			int dir;
			if(rem[p - 1] < rem[p]){ dir = 1; }
			else if(rem[p - 1] > rem[p]){ dir = -1; }
			else{ dir = prvdir; }
			if(dir != prvdir){
				ord.push_back(p);
				used[p] = 1;
			}
			if(dir == 1){
				--rem[p];
				++p;
			}
			else{
				--p;
				--rem[p];
			}
			prvdir = dir;
		}
		ord.push_back(p);
		used[p] = 1;

		vector<int> ans2 = {ord[0]};
		for(int i = 1; i < (int)ord.size(); ++i){
			int x = ord[i - 1], y = ord[i];
			int d = x < y ? 1 : -1;
			for(int j = x + d; j != y; j += d){
				if(!used[j]){
					used[j] = 1;
					ans2.push_back(j);
				}
			}
			ans2.push_back(y);
		}
		
		char spr = '\n';
		cout << ans1;
		for(int x : ans2){
			cout << spr << x;
			spr = ' ';
		}
		cout << endl;
	}
}
accuracy: 1
time_ms: 2360
memory_kb: 178084
final_rank: 2

code_snippet: // (⁠◕⁠ᴗ⁠◕⁠✿⁠)

// #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define srep(i, s, n) for (ll i = s; i < (n); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using vvv = vv<vc<T>>;
using vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;
using ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;
using ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;
using uint = unsigned int;
using ull = unsigned long long;
const ld pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
// const ll mod = 1000000007;
const ll mod = 998244353;
inline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }

#define debug(var)  do{std::cout << #var << " : \n";view(var);}while(0)
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }

void solve(int n){
    vl C(n, 0);rep(i, n - 1) cin >> C[i];
    vvvl dp(n + 1, vvl(n + 1, vl(3, -INF)));
    dp[0][0][0] = 0;
    rep(i, n) rep(j, n + 1) rep(k, 3){
        if (j > 0) dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k] + C[i] * j);
        if (j - 2 + (i == n - 1) > 0) dp[i + 1][j - 2][k] = max(dp[i + 1][j - 2][k], dp[i][j][k] + C[i] * (j - 2));
        if (j + 2 < n + 1) dp[i + 1][j + 2][k] = max(dp[i + 1][j + 2][k], dp[i][j][k] + C[i] * (j + 2));
        if (k < 2){
            if (j - 1 + (i == n - 1) > 0) dp[i + 1][j - 1][k + 1] = max(dp[i + 1][j - 1][k + 1], dp[i][j][k] + C[i] * (j - 1));
            if (j + 1 < n + 1) dp[i + 1][j + 1][k + 1] = max(dp[i + 1][j + 1][k + 1], dp[i][j][k] + C[i] * (j + 1));
        }
    }
    ll cost = dp[n][0][2];
    cout << cost << endl;
    int lastj = 0, lastk = 2;
    vi A = {0};
    for (int i = n - 1; i >= 0; i--){
        if (dp[i + 1][lastj][lastk] == dp[i][lastj][lastk] + C[i] * lastj){
            A.push_back(lastj);
        }else if (lastj + 2 < n + 1 && dp[i + 1][lastj][lastk] == dp[i][lastj + 2][lastk] + C[i] * lastj){
            A.push_back(lastj + 2);
            lastj += 2;
        }else if (lastj - 2 >= 0 && dp[i + 1][lastj][lastk] == dp[i][lastj - 2][lastk] + C[i] * lastj){
            A.push_back(lastj - 2);
            lastj -= 2;
        }else if (lastk > 0 && lastj + 1 < n + 1 && dp[i + 1][lastj][lastk] == dp[i][lastj + 1][lastk - 1] + C[i] * lastj){
            A.push_back(lastj + 1);
            lastj++;
            lastk--;
        }else if (lastk > 0 && lastj - 1 >= 0 && dp[i + 1][lastj][lastk] == dp[i][lastj - 1][lastk - 1] + C[i] * lastj){
            A.push_back(lastj - 1);
            lastj--;
            lastk--;
        }else{
            // assert(false);
        }
    }
    reverse(all(A));
    int s;
    rep(i, n) if (abs(A[i] - A[i + 1]) == 1){
        s = i;
        break;
    }
    vi path;
    while (true){
        path.push_back(s);
        if (A[s] > A[s + 1]){
            A[s]--;
            s--;
            while (abs(A[s] - A[s + 1]) % 2 == 0 && A[s] > 0 && s > 0){
                path.push_back(s);
                A[s]--;
                s--;
            }
        }else if (A[s] < A[s + 1]){
            A[s + 1]--;
            s++;
            while (abs(A[s] - A[s + 1]) % 2 == 0 && A[s + 1] > 0 && s < n - 1){
                path.push_back(s);
                A[s + 1]--;
                s++;
            }
        }else if (A[s] == A[s + 1] && A[s] != 0){
            A[s]--;
            s--;
            while (abs(A[s] - A[s + 1]) % 2 == 0 && A[s] > 0 && s > 0){
                path.push_back(s);
                A[s]--;
                s--;
            }
        }else break;
    }
    vi use(len(path), 0);
    vi seen(n, 0);
    use[0] = 1;
    seen[path[0]] = 1;
    use[len(path) - 1] = 1;
    seen[path.back()] = 1;
    srep(i, 1, len(path) - 1) if (path[i - 1] == path[i + 1]){
        seen[path[i]] = 1;
        use[i] = 1;
    }
    rep(i, len(path)) if (!seen[path[i]]){
        seen[path[i]] = 1;
        use[i] = 1;
    }
    vi ans;
    rep(i, len(path)) if (use[i]) ans.push_back(path[i]);
    assert(len(ans) == n);
    rep(i, n) cout << ans[i] + 1 << " \n"[i == n - 1];
}
int main(){
    while (true){
        int n; cin >> n;
        if (n == 0) break;
        solve(n);
    }
}
accuracy: 1
time_ms: 6890
memory_kb: 522732
final_rank: 16

code_snippet: #pragma GCC optimeze("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(v) v.begin(),v.end()
#define dbg(x) cerr << #x << ": " << (x) << endl;
template<class F, class S>
ostream& operator<<(ostream& os, pair<F,S>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template<class Iter>
void print(Iter beg, Iter end) {
    for (Iter itr = beg; itr != end; ++itr) {
        cerr << *itr << ' ';
    }
    cerr << '\n';
}
struct UF {
    UF(int n) : par(n), rank(n){
        iota(ALL(par), 0);
    }
    int root(int x) {
        if (par[x] == x) return par[x];
        return par[x] = root(par[x]);
    }
    int unite(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;

        if (rank[x] < rank[y]) swap(x,y);
        par[y] = x;
        if (rank[x] == rank[y]) ++rank[x];
        return true;
    }
    int same(int x, int y) {
        return root(x) == root(y);
    }
    vector<int> rank, par;
};

void solve1() {
    int n;
    cin >> n;
    if (n==0) exit(0);
    vector<ll> c(n+1);
    for (int i = 2; i <= n; ++i) {
        cin >> c[i];
        c[i] += c[i-1];
    }
    vector dp(1<<n, vector<ll>(n, LLONG_MIN));
    for (int i = 0; i < n; ++i) {
        dp[1 << i][i] = 0;
    }
    for (int i = 0; i < 1<<n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (!(i >> j & 1)) continue;
            for (int k = 0; k < n; ++k) {
                if (i >> k & 1) continue;
                int next = i | (1 << k);
                int l = min(j+1, k+1);
                int r = max(j+1, k+1);
                dp[next][k] = max(dp[next][k], dp[i][j] + (c[r] - c[l]));
            }
        }
    }
    ll ans = LLONG_MIN;
    for (int i = 0; i < n; ++i) {
        ans = max(ans, dp[(1 << n) - 1][i]);
    }
    cout << ans << '\n';
}
void solve() {
    int n;
    cin >> n;
    if (n==0) exit(0);
    vector<ll> c(n+1);
    for (int i = 2; i <= n; ++i) {
        cin >> c[i];
        c[i] += c[i-1];
    }
    vector<array<ll,3>> edges;
    edges.reserve((n * (n-1) / 2));
    for (int i = 1; i <= n; ++i) {
        for (int j = i+1; j <= n; ++j) {
            edges.push_back({c[j]-c[i], i-1, j-1});
        }
    }
    sort(ALL(edges), greater<array<ll,3>>());
    vector<vector<int>> g(n);
    UF uf(n);
    int cnt = 0;
    ll ans = 0;
    for (auto&& [c,l,r] : edges) {
        if (cnt >= n-1) continue;
        if (uf.same(l, r)) continue;
        if (g[l].size() >= 2 || g[r].size() >= 2) continue;
        g[l].push_back(r);
        g[r].push_back(l);
        uf.unite(l, r);
        cnt++;
        ans += c;
    }
    int now = -1;
    for (int i = 0; i < n; ++i) {
        if (g[i].size() == 1) {
            now = i;
            break;
        }
    }
    vector<int> path;
    path.push_back(now);
    while (path.size() < n) {
        int pre = (path.size() <= 1 ? -1 : path[ path.size()-2 ]);
        for (int to : g[path.back()]) {
            if (pre == to) continue;
            path.push_back(to);
            break;
        }
    }
    cout << ans << '\n';
    for (int i = 0; i < n; ++i) {
        cout << path[i]+1 << " \n"[i==n-1];
    }
}

void solve2() {
    int n;
    cin >> n;
    if (n==0) exit(0);
    vector<ll> c(n);
    for (int i = 1; i < n; ++i) {
        cin >> c[i];
        c[i] += c[i-1];
    }

    ll inf = 4e18;
    vector dp(3, vector(n+1, vector<ll>(n+1, -inf)));
    vector rdp(3, vector(n+1, vector<int>(n+1, -1)));
    dp[0][0][0] = 0;
    for (int k = 0; k <= 2; ++k) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j <= n; ++j) {
                if (dp[k][i][j] == -inf) continue;
                // L, L
                if (j+2 <= n && dp[k][i][j] - 2 * c[i] > dp[k][i+1][j+2]) {
                    dp[k][i+1][j+2] = dp[k][i][j] - 2 * c[i];
                    rdp[k][i+1][j+2] = 0;
                }
                // L, R
                if (j >= 1 && dp[k][i][j] > dp[k][i+1][j]) {
                    dp[k][i+1][j] = dp[k][i][j];
                    rdp[k][i+1][j] = 1;
                }
                // R, R
                if (j >= 2 && i+1==n && dp[k][i][j] + 2 * c[i] > dp[k][i+1][j-2]) {
                    dp[k][i+1][j-2] = dp[k][i][j] + 2 * c[i];
                    rdp[k][i+1][j-2] = 2;
                }
                else if (j >= 3 && i+1<n && dp[k][i][j] + 2 * c[i] > dp[k][i+1][j-2]) {
                    dp[k][i+1][j-2] = dp[k][i][j] + 2 * c[i];
                    rdp[k][i+1][j-2] = 2;
                }

                //　始点か終点
                // L
                if (j+1 <= n && k < 2 && dp[k][i][j] - c[i] > dp[k+1][i+1][j+1]) {
                    dp[k+1][i+1][j+1] = dp[k][i][j] - c[i];
                    rdp[k+1][i+1][j+1] = 3;
                }
                // R
                if (j >= 1 && i+1==n && k < 2 && dp[k][i][j] + c[i] > dp[k+1][i+1][j-1]) {
                    dp[k+1][i+1][j-1] = dp[k][i][j] + c[i];
                    rdp[k+1][i+1][j-1] = 4;
                }
                else if (j >= 2 && i+1<n && k < 2 && dp[k][i][j] + c[i] > dp[k+1][i+1][j-1]) {
                    dp[k+1][i+1][j-1] = dp[k][i][j] + c[i];
                    rdp[k+1][i+1][j-1] = 4;
                }
            }
        }
    }
    // reconstruct
    vector<int> id(n);
    int j=0, k=2;
    int now = -1;
    for (int i = n; i >= 1; --i) {
        id[i-1] = rdp[k][i][j];
        if (rdp[k][i][j] == 0) {
            j-=2;
        } else if (rdp[k][i][j] == 1) {

        } else if (rdp[k][i][j] == 2) {
            j += 2;
        } else if (rdp[k][i][j] == 3) {
            j--;
            k--;
            now = i-1;
        } else if (rdp[k][i][j] == 4) {
            j++;
            k--;
            now = i-1;
        }
    }
    assert(j == 0);
    assert(k == 0);
    assert(now >= 0);


    vector<int> path;
    vector<bool> used(n,false);

    path.push_back(now);
    used[now] = true;
    while (path.size() < n) {
        int dir = (id[now]==0 || id[now]==3 ? 1 : -1);
        for (int i = now; 0 <= i && i < n; i += dir) {
            if (used[i]) continue;
            if (id[i] == 1) {
                path.push_back(i);
                used[i] = true;
                continue;
            }
            if (dir==1 && id[i]==2) {
                now = i;
                break;
            }
            if (dir==1 && id[i]==4 && path.size()==n-1) {
                now = i;
                break;
            }
            if (dir==-1 && id[i]==0) {
                now = i;
                break;
            }
            if (dir==-1 && id[i]==3 && path.size()==n-1) {
                now = i;
                break;
            }
        }
        path.push_back(now);
        used[now] = true;
    }
    cout << dp[2][n][0] << '\n';
    // print(ALL(id));
    for (int i = 0; i < n; ++i) {
        cout << path[i]+1 << " \n"[i==n-1];
    }
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    while (true) {
        solve2();
    }
}
accuracy: 1
time_ms: 2530
memory_kb: 352064
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(v) v.begin(),v.end()
#define dbg(x) cerr << #x << ": " << (x) << endl;
template<class F, class S>
ostream& operator<<(ostream& os, pair<F,S>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template<class Iter>
void print(Iter beg, Iter end) {
    for (Iter itr = beg; itr != end; ++itr) {
        cerr << *itr << ' ';
    }
    cerr << '\n';
}
struct UF {
    UF(int n) : par(n), rank(n){
        iota(ALL(par), 0);
    }
    int root(int x) {
        if (par[x] == x) return par[x];
        return par[x] = root(par[x]);
    }
    int unite(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;

        if (rank[x] < rank[y]) swap(x,y);
        par[y] = x;
        if (rank[x] == rank[y]) ++rank[x];
        return true;
    }
    int same(int x, int y) {
        return root(x) == root(y);
    }
    vector<int> rank, par;
};

void solve1() {
    int n;
    cin >> n;
    if (n==0) exit(0);
    vector<ll> c(n+1);
    for (int i = 2; i <= n; ++i) {
        cin >> c[i];
        c[i] += c[i-1];
    }
    vector dp(1<<n, vector<ll>(n, LLONG_MIN));
    for (int i = 0; i < n; ++i) {
        dp[1 << i][i] = 0;
    }
    for (int i = 0; i < 1<<n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (!(i >> j & 1)) continue;
            for (int k = 0; k < n; ++k) {
                if (i >> k & 1) continue;
                int next = i | (1 << k);
                int l = min(j+1, k+1);
                int r = max(j+1, k+1);
                dp[next][k] = max(dp[next][k], dp[i][j] + (c[r] - c[l]));
            }
        }
    }
    ll ans = LLONG_MIN;
    for (int i = 0; i < n; ++i) {
        ans = max(ans, dp[(1 << n) - 1][i]);
    }
    cout << ans << '\n';
}
void solve() {
    int n;
    cin >> n;
    if (n==0) exit(0);
    vector<ll> c(n+1);
    for (int i = 2; i <= n; ++i) {
        cin >> c[i];
        c[i] += c[i-1];
    }
    vector<array<ll,3>> edges;
    edges.reserve((n * (n-1) / 2));
    for (int i = 1; i <= n; ++i) {
        for (int j = i+1; j <= n; ++j) {
            edges.push_back({c[j]-c[i], i-1, j-1});
        }
    }
    sort(ALL(edges), greater<array<ll,3>>());
    vector<vector<int>> g(n);
    UF uf(n);
    int cnt = 0;
    ll ans = 0;
    for (auto&& [c,l,r] : edges) {
        if (cnt >= n-1) continue;
        if (uf.same(l, r)) continue;
        if (g[l].size() >= 2 || g[r].size() >= 2) continue;
        g[l].push_back(r);
        g[r].push_back(l);
        uf.unite(l, r);
        cnt++;
        ans += c;
    }
    int now = -1;
    for (int i = 0; i < n; ++i) {
        if (g[i].size() == 1) {
            now = i;
            break;
        }
    }
    vector<int> path;
    path.push_back(now);
    while (path.size() < n) {
        int pre = (path.size() <= 1 ? -1 : path[ path.size()-2 ]);
        for (int to : g[path.back()]) {
            if (pre == to) continue;
            path.push_back(to);
            break;
        }
    }
    cout << ans << '\n';
    for (int i = 0; i < n; ++i) {
        cout << path[i]+1 << " \n"[i==n-1];
    }
}

void solve2() {
    int n;
    cin >> n;
    if (n==0) exit(0);
    vector<ll> c(n);
    for (int i = 1; i < n; ++i) {
        cin >> c[i];
        c[i] += c[i-1];
    }

    ll inf = 4e18;
    vector dp(3, vector(n+1, vector<ll>(n+1, -inf)));
    vector rdp(3, vector(n+1, vector<int>(n+1, -1)));
    dp[0][0][0] = 0;
    for (int k = 0; k <= 2; ++k) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j <= n; ++j) {
                if (dp[k][i][j] == -inf) continue;
                // L, L
                if (j+2 <= n && dp[k][i][j] - 2 * c[i] > dp[k][i+1][j+2]) {
                    dp[k][i+1][j+2] = dp[k][i][j] - 2 * c[i];
                    rdp[k][i+1][j+2] = 0;
                }
                // L, R
                if (j >= 1 && dp[k][i][j] > dp[k][i+1][j]) {
                    dp[k][i+1][j] = dp[k][i][j];
                    rdp[k][i+1][j] = 1;
                }
                // R, R
                if (j >= 2 && i+1==n && dp[k][i][j] + 2 * c[i] > dp[k][i+1][j-2]) {
                    dp[k][i+1][j-2] = dp[k][i][j] + 2 * c[i];
                    rdp[k][i+1][j-2] = 2;
                }
                else if (j >= 3 && i+1<n && dp[k][i][j] + 2 * c[i] > dp[k][i+1][j-2]) {
                    dp[k][i+1][j-2] = dp[k][i][j] + 2 * c[i];
                    rdp[k][i+1][j-2] = 2;
                }

                //　始点か終点
                // L
                if (j+1 <= n && k < 2 && dp[k][i][j] - c[i] > dp[k+1][i+1][j+1]) {
                    dp[k+1][i+1][j+1] = dp[k][i][j] - c[i];
                    rdp[k+1][i+1][j+1] = 3;
                }
                // R
                if (j >= 1 && i+1==n && k < 2 && dp[k][i][j] + c[i] > dp[k+1][i+1][j-1]) {
                    dp[k+1][i+1][j-1] = dp[k][i][j] + c[i];
                    rdp[k+1][i+1][j-1] = 4;
                }
                else if (j >= 2 && i+1<n && k < 2 && dp[k][i][j] + c[i] > dp[k+1][i+1][j-1]) {
                    dp[k+1][i+1][j-1] = dp[k][i][j] + c[i];
                    rdp[k+1][i+1][j-1] = 4;
                }
            }
        }
    }
    // reconstruct
    vector<int> id(n);
    int j=0, k=2;
    int now = -1;
    for (int i = n; i >= 1; --i) {
        id[i-1] = rdp[k][i][j];
        if (rdp[k][i][j] == 0) {
            j-=2;
        } else if (rdp[k][i][j] == 1) {

        } else if (rdp[k][i][j] == 2) {
            j += 2;
        } else if (rdp[k][i][j] == 3) {
            j--;
            k--;
            now = i-1;
        } else if (rdp[k][i][j] == 4) {
            j++;
            k--;
            now = i-1;
        }
    }
    assert(j == 0);
    assert(k == 0);
    assert(now >= 0);


    vector<int> path;
    vector<bool> used(n,false);

    path.push_back(now);
    used[now] = true;
    while (path.size() < n) {
        int dir = (id[now]==0 || id[now]==3 ? 1 : -1);
        for (int i = now; 0 <= i && i < n; i += dir) {
            if (used[i]) continue;
            if (id[i] == 1) {
                path.push_back(i);
                used[i] = true;
                continue;
            }
            if (dir==1 && id[i]==2) {
                now = i;
                break;
            }
            if (dir==1 && id[i]==4 && path.size()==n-1) {
                now = i;
                break;
            }
            if (dir==-1 && id[i]==0) {
                now = i;
                break;
            }
            if (dir==-1 && id[i]==3 && path.size()==n-1) {
                now = i;
                break;
            }
        }
        path.push_back(now);
        used[now] = true;
    }
    cout << dp[2][n][0] << '\n';
    // print(ALL(id));
    for (int i = 0; i < n; ++i) {
        cout << path[i]+1 << " \n"[i==n-1];
    }
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    while (true) {
        solve2();
    }
}
accuracy: 1
time_ms: 2520
memory_kb: 352148
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef pair<ll,ll> pi;
typedef pair<ll,pi> ppi;
#define FOR(i,l,r) for(ll i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)
#define RREP(i,n) RFOR(i,0,n)
#define sz(A) (ll)(A.size())
#define ALL(A) A.begin(),A.end()
#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())
#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())
#define COU(A,x) (UB(A,x)-LB(A,x))
#define F first
#define S second
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<" "<<p.S;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}
const ll mod=998244353;
template<const long long int mod=998244353>
struct modint{
  using mint=modint<mod>;
  long long int x;
  modint(long long int _x=0):x(_x%mod){if(x<0)x+=mod;}
  long long int val(){return x;}
  mint&operator=(const mint&a){x=a.x;return *this;}
  mint&operator+=(const mint&a){x+=a.x;if(x>=mod)x-=mod;return *this;}
  mint&operator-=(const mint&a){x-=a.x;if(x<0)x+=mod;return *this;}
  mint&operator*=(const mint&a){x*=a.x;x%=mod;return *this;}
  friend mint operator+(const mint&a,const mint&b){return mint(a)+=b;}
  friend mint operator-(const mint&a,const mint&b){return mint(a)-=b;}
  friend mint operator*(const mint&a,const mint&b){return mint(a)*=b;}
  mint operator-()const{return mint(0)-*this;}
  mint pow(long long int n){
    if(!n)return 1;
    mint a=1;
    mint _x=x;
    while(n){
      if(n&1)a*=_x;
      _x=_x*_x;n>>=1;
    }
    return a;
  }
  mint inv(){return pow(mod-2);}
  mint&operator/=(mint&a){return *this*=a.inv();}
  friend mint operator/(const mint&a,mint b){return mint(a)/=b;}
};
using mint=modint<998244353>;
int main(){
    while(1){
        ll N;cin>>N;
        if(!N)return 0;
        vi A(N-1);cin>>A;
        vvvi DP(N,vvi(N+10,vi(3,-1e18)));
        DP[0][1][1]=0;
        DP[0][2][0]=0;
        REP(i,N-1)FOR(j,1,N+1)REP(k,3){
            //2
            chmax(DP[i+1][j+2][k],DP[i][j][k]+A[i]*j);
            //1
            if(k<2)chmax(DP[i+1][j+1][k+1],DP[i][j][k]+A[i]*j);
            //0
            chmax(DP[i+1][j][k],DP[i][j][k]+A[i]*j);
            //-1
            if(k<2)chmax(DP[i+1][j-1][k+1],DP[i][j][k]+A[i]*j);
            //-2
            if(j>=2)chmax(DP[i+1][j-2][k],DP[i][j][k]+A[i]*j);
        }
        cout<<DP[N-1][0][2]<<endl;
        vi T(N-1);
        {
            ll x=0,y=2;
            RREP(i,N-1){
                if(DP[i][x+2][y]+A[i]*(x+2)==DP[i+1][x][y])T[i]=-2,x+=2;
                else if(y&&DP[i][x+1][y-1]+A[i]*(x+1)==DP[i+1][x][y])T[i]=-1,x++,y--;
                else if(DP[i][x][y]+A[i]*x==DP[i+1][x][y])T[i]=0;
                else if(y&&DP[i][x-1][y-1]+A[i]*(x-1)==DP[i+1][x][y])T[i]=1,x--,y--;
                else{
                    assert(DP[i][x-2][y]+A[i]*(x-2)==DP[i+1][x][y]);
                    T[i]=2,x-=2;
                }
            }
            T.insert(T.begin(),x);
        }
        ll v=0;
        while(abs(T[v])!=1)v++;
        vi P={v+1};
        vb used(N);
        used[v]=1;
        while(count(ALL(used),0)){
            if(T[v]>0){
                while(v<N&&(used[v]||T[v]>0||abs(T[v])==1))v++;
                if(v<N)T[v]++;
                else{
                    assert(count(ALL(used),0)==1);
                    v=0;
                    while(used[v])v++;
                }
            }
            else{
                while(v>=0&&(used[v]||T[v]<0||abs(T[v])==1))v--;
                if(v>=0)T[v]--;
                else{
                    assert(count(ALL(used),0)==1);
                    v=0;
                    while(used[v])v++;
                }
            }
            used[v]=1;
            P.emplace_back(v+1);
        }
        assert(sz(P)==N);
        cout<<P<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 6440
memory_kb: 492332
final_rank: 13

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

template <unsigned mod = 1000000007> struct fp {
    unsigned v;
    static constexpr int get_mod() {
        return mod;
    }
    constexpr unsigned inv() const {
        assert(v != 0);
        int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;
        while (y > 0) {
            t = x / y;
            x -= t * y, p -= t * q;
            tmp = x, x = y, y = tmp;
            tmp = p, p = q, q = tmp;
        }
        if (p < 0)
            p += mod;
        return p;
    }
    constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}
    fp operator-() const {
        return fp() - *this;
    }
    fp pow(ull t) {
        fp res = 1, b = *this;
        while (t) {
            if (t & 1)
                res *= b;
            b *= b;
            t >>= 1;
        }
        return res;
    }
    fp &operator+=(const fp &x) {
        if ((v += x.v) >= mod)
            v -= mod;
        return *this;
    }
    fp &operator-=(const fp &x) {
        if ((v += mod - x.v) >= mod)
            v -= mod;
        return *this;
    }
    fp &operator*=(const fp &x) {
        v = ull(v) * x.v % mod;
        return *this;
    }
    fp &operator/=(const fp &x) {
        v = ull(v) * x.inv() % mod;
        return *this;
    }
    fp operator+(const fp &x) const {
        return fp(*this) += x;
    }
    fp operator-(const fp &x) const {
        return fp(*this) -= x;
    }
    fp operator*(const fp &x) const {
        return fp(*this) *= x;
    }
    fp operator/(const fp &x) const {
        return fp(*this) /= x;
    }
    bool operator==(const fp &x) const {
        return v == x.v;
    }
    bool operator!=(const fp &x) const {
        return v != x.v;
    }
    friend istream &operator>>(istream &is, fp &x) {
        return is >> x.v;
    }
    friend ostream &operator<<(ostream &os, const fp &x) {
        return os << x.v;
    }
};

template <unsigned mod> void rd(fp<mod> &x) {
    fastio::rd(x.v);
}
template <unsigned mod> void wt(fp<mod> x) {
    fastio::wt(x.v);
}

template <typename T> T Inv(ll n) {
    static const int md = T::get_mod();
    static vector<T> buf({0, 1});
    assert(n > 0);
    n %= md;
    while (SZ(buf) <= n) {
        int k = SZ(buf), q = (md + k - 1) / k;
        buf.push_back(buf[k * q - md] * q);
    }
    return buf[n];
}

template <typename T> T Fact(ll n, bool inv = 0) {
    static const int md = T::get_mod();
    static vector<T> buf({1, 1}), ibuf({1, 1});
    assert(n >= 0 and n < md);
    while (SZ(buf) <= n) {
        buf.push_back(buf.back() * SZ(buf));
        ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));
    }
    return inv ? ibuf[n] : buf[n];
}

template <typename T> T nPr(int n, int r, bool inv = 0) {
    if (n < 0 || n < r || r < 0)
        return 0;
    return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);
}
template <typename T> T nCr(int n, int r, bool inv = 0) {
    if (n < 0 || n < r || r < 0)
        return 0;
    return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);
}
template <typename T> T nHr(int n, int r, bool inv = 0) {
    return nCr<T>(n + r - 1, r, inv);
}

/**
 * @brief Modint
 */

void DFS(int N, vector<int>& V, int S, vector<int>& Ret) {
    if (N == 2) {
        if (S == 0) Ret.push_back(1), Ret.push_back(0);
        else Ret.push_back(0), Ret.push_back(1);
        return;
    }
    if (V[S]<V[S+1]) {
        int Cur = S;
        while(1) {
            if (V[Cur+2]-V[Cur+1]==-2 || V[Cur+2]-V[Cur+1]==0) {
                V[Cur+1]--;
                Cur++;
                V.erase(V.begin()+S);
                DFS(N-1,V,Cur-1,Ret);
                rep(i,0,Ret.size()) if (Ret[i] >= S) Ret[i]++;
                Ret.push_back(S);
                return;
            }
            V[Cur+1]--;
            Cur++;
        }
    }
    else {
        int Cur = S;
        while(1) {
            if (V[Cur-1]-V[Cur]==-2 || V[Cur-1]-V[Cur]==0) {
                V[Cur]--;
                Cur--;
                V.erase(V.begin()+S);
                DFS(N-1,V,Cur,Ret);
                rep(i,0,Ret.size()) if (Ret[i] >= S) Ret[i]++;
                Ret.push_back(S);
                return;
            }
            V[Cur]--;
            Cur--;
        }
    }
}

static ll DP[3000][3001][3];
static pair<int,int> Pre[3000][3001][3];

int main() {
while(1) {
    int N;
    cin >> N;
    if (N == 0) return 0;
    vector<ll> C(N-1);
    rep(i,0,N-1) cin >> C[i];
    int MAXS = N+1;
    vector<vector<vector<ll>>> DP(N,vector<vector<ll>>(MAXS,vector<ll>(3,-INF)));
    rep(i,0,N) rep(j,0,MAXS) rep(k,0,3) DP[i][j][k] = -INF;
    DP[0][0][0] = 0;
    rep(i,0,N-1) {
        rep(j,0,MAXS) {
            rep(k,0,3) {
                if (DP[i][j][k] == -INF) continue;
                for (int d = -2; d <= 2; d += 2) {
                    ll nj = j + d;
                    if (0 < nj && nj < MAXS) {
                        if (chmax(DP[i+1][nj][k], DP[i][j][k] + C[i] * nj)) Pre[i+1][nj][k] = {j,k};
                    }
                }
                if (k != 2) {
                    for (int d = -1; d <= 1; d += 2) {
                        ll nj = j + d;
                        if (0 < nj && nj < MAXS) {
                            if (chmax(DP[i+1][nj][k+1], DP[i][j][k] + C[i] * nj)) Pre[i+1][nj][k+1] = {j,k};
                        }
                    }
                }
            }
        }
    }
    ll ANS = max(DP[N-1][2][2], DP[N-1][1][1]);
    pair<int,int> Now;
    if (DP[N-1][2][2] >= DP[N-1][1][1]) Now = {2,2};
    else Now = {1,1};
    vector<int> V;
    for (int i = N-1; i >= 1; i--) {
        V.push_back(Now.first);
        Now = Pre[i][Now.first][Now.second];
    }
    V.push_back(0);
    reverse(V.begin(), V.end());
    V.push_back(0);
    int S = -1;
    rep(i,0,N) {
        if (abs(V[i+1]-V[i])==1) {
            S = i;
            break;
        }
    }
    vector<int> ANSV;
    DFS(N,V,S, ANSV);
    reverse(ANSV.begin(), ANSV.end());
    cout << ANS << endl;
    rep(i,0,N) cout << ANSV[i]+1 << (i == N-1 ? '\n' : ' ');
}
}
accuracy: 1
time_ms: 6920
memory_kb: 700280
final_rank: 18

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

template < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }
template < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }
template < class T > vector< T >  operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }
template < class T > vector< T >  operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

void solve(int N) {
    vector<i64> C = in(N - 1);
    const i64 INF = 1e18;
    const int M = (N + 1) / 2;
    vector dp(N - 1, vector(M + 1, vector(3, -INF)));
    const vector<pair<int,int>> edge = {
        {0, 0}, {0, 1}, {1, 1}, {1, 2}, {2, 2}
    };
    dp[0][1][0] = C[0] * 2;
    dp[0][1][1] = C[0] * 1;
    for(int i : rep(1, N - 1)) {
        for(int x = 1; x <= M; x++) {
            for(int nx = x - 1; nx <= x + 1; nx++) {
                if(1 <= nx and nx <= M) {
                    for(auto &[y, ny] : edge) {
                        if(abs((2 * x - (y == 1)) - (2 * nx - (ny == 1))) <= 2) {
                            chmax(dp[i][nx][ny], dp[i - 1][x][y] + C[i] * i64(2 * nx - (ny == 1)));
                        }
                    }
                }
            }
        }
    }

    i64 Max = -INF;
    tuple<int,int,int> argMax = {-1, -1, -1};
    if(chmax(Max, dp[(N - 1) - 1][1][1])) argMax = {(N - 1) - 1, 1, 1};
    if(chmax(Max, dp[(N - 1) - 1][1][2])) argMax = {(N - 1) - 1, 1, 2};
    i64 ans_score = Max;
    vector<int> deg(N - 1);
    deg[(N - 1) - 1] = 2 - (get<2>(argMax) == 1);
    for(int i = (N - 1) - 2; i >= 0; i--) {
        [&] {
            for(int x = 1; x <= M; x++) {
                for(int nx = x - 1; nx <= x + 1; nx++) {
                    if(1 <= nx and nx <= M) {
                        for(auto [y, ny] : edge) {
                            if(abs((2 * x - (y == 1)) - (2 * nx - (ny == 1))) <= 2) {
                                if(dp[i + 1][nx][ny] == dp[i][x][y] + C[i + 1] * i64(2 * nx - (ny == 1)) and dp[i + 1][nx][ny] == Max) {
                                    if(make_tuple(i + 1, nx, ny) == argMax) {
                                        Max = dp[i][x][y];
                                        argMax = {i, x, y};
                                        deg[i] = 2 * x - (y == 1);
                                        return;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            assert(0);
        }();
    }

    deg.insert(deg.begin(), {0});
    deg.insert(deg.end(), {0});
    vector<int> dead(N + 1, 0);
    vector<int> ans;
    for(int i = 1; i <= N; i++) {
        if(abs(deg[i] - deg[i - 1]) == 1) {
            ans.push_back(i);
            dead[i] = true;
            break;
        }
    }
    for(int _ : rep(N - 2)) {
        const int i = ans.back();
        if(deg[i - 1] < deg[i]) { // -> 
            for(int ni = i + 1; ni <= N; ni++) if(not dead[ni]) {
                int d = deg[ni] - deg[ni - 1];
                if(d == 0 or d == -2) {
                    ans.push_back(ni);
                    dead[ni] = true;
                    for(int k = i; k < ni; k++) deg[k] -= 1;
                    break;
                }
            }
        } else { // <-
            for(int ni = i - 1; ni >= 1; ni--) if(not dead[ni]) {
                int d = deg[ni - 1] - deg[ni];
                if(d == 0 or d == -2) {
                    ans.push_back(ni);
                    dead[ni] = true;
                    for(int k = ni; k < i; k++) deg[k] -= 1;
                    break;
                }
            }
        }
    }
    for(int i = 1; i <= N; i++) if(not dead[i]) ans.push_back(i);
    print(ans_score);
    print(ans);
}

int main() {
    while(true) {
        int N = in();
        if(N == 0) return 0;
        solve(N);
    }
}
accuracy: 1
time_ms: 4570
memory_kb: 247032
final_rank: 7

code_snippet: #include<bits/stdc++.h>
#include<cassert>

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
#define rep(i,n) for(ll i=0;i<ll(n);i++)
#define all(A) A.begin(),A.end()

void chmax(ll& p, ll q) { p = max(p, q); };
void chmin(ll& p, ll q) { p = min(p, q); };

void solve(ll N) {
    vll C(N + 1, 0);
    rep(i, N - 1)cin >> C[i + 1];
    vector<vector<vector<pair<ll, int>>>> DP(N + 1, vector<vector<pair<ll, int>>>(N + 5, vector<pair<ll, int>>(3, { -1e18,-1e9 })));
    DP[0][0][0] = { 0,-1 };
    rep(i, N)rep(j,min(N + 5,i*2+2))rep(k, 3){
        if(DP[i][j][k].first<-1e17)continue;
        for (ll p = -2; p <= 2; p++) {
            ll nk = k, nj = j + p;
            if (abs(p) == 1)nk++;
            if (nk == 3 || nj < 0 || nj>N+4)continue;
            if (i != N - 1 && nj == 0)continue;
            ll nc = DP[i][j][k].first + C[i + 1] * nj;
            if (nc > DP[i + 1][nj][nk].first)DP[i + 1][nj][nk] = { nc,p };
        }
    }
    vll D(N + 1, 0);
    ll p = 2;
    for (ll i = N - 1; i >= 0; i--) {
        D[i] = D[i + 1] - DP[i + 1][D[i + 1]][p].second;
        if (abs(D[i] - D[i + 1]) == 1)p--;
    }
    cout << DP[N][0][2].first << endl;
    // rep(i, N + 1)cout << D[i] << " \n"[i == N];

    vll AN(N);
    vector<bool> seen(N, 0);
    rep(i, N)if (abs(D[i] - D[i + 1]) == 1) {
        AN[0] = i;
        seen[i] = 1;
        break;
    }
    rep(i, N - 2) {
        ll nw = AN[i];
        seen[nw] = 1;
        if (D[nw] < D[nw + 1]) {
            for (ll j = nw + 1; j <= N; j++) {
                if ((D[j] == D[j + 1] || D[j] == D[j + 1] + 2) && !seen[j]) {
                    for (ll p = nw + 1; p <= j; p++) {
                        D[p]--;
                    }
                    AN[i + 1] = j;
                    break;
                }
            }
        }
        else {
            for (ll j = nw - 1; j >= 0; j--) {
                if ((D[j] == D[j + 1] || D[j] == D[j + 1] - 2) && !seen[j]) {
                    for (ll p = nw; p > j; p--) {
                        D[p]--;
                    }
                    AN[i + 1] = j;
                    break;
                }
            }
        }
    }
    seen[AN[N-2]]=1;
    rep(i, N)if (!seen[i])AN[N - 1] = i;
    rep(i, N)cout << AN[i] + 1 << " \n"[i == N - 1];
}

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N;
    while (cin >> N) {
        if (N == 0)return 0;
        solve(N);
    }
}
accuracy: 1
time_ms: 7910
memory_kb: 770864
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;

template <typename T>
bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}

int main()
{
  while (true)
  {
    int n;
    cin >> n;
    if (n == 0)
      break;

    vector<long long> a(n);
    for (int i = 0; i < n - 1; ++i)
      cin >> a[i];

    const long long inf = 1LL << 50;
    vector<vector<vector<long long>>> dp(3, vector<vector<long long>>(n, vector<long long>(n, -inf)));
    dp[0][0][0] = 0;

    for (int i = 0; i < n - 1; ++i)
    {
      for (int j = 0; j < n; ++j)
      {
        if (j > 2 * i || j > 2 * (n - i))
          continue;
        for (int k = 0; k < 3; ++k)
        {
          if (dp[k][i][j] == -inf)
            continue;
          for(int dj = -2; dj<=2;dj+=2){
            int nj = j + dj;
            int nk = k + 0;
            if (nj > 0 && nj < n)
            {
              chmax(dp[nk][i + 1][nj], dp[k][i][j] + a[i] * nj);
            }
          }
          if (k == 2)
            continue;
          for(int dj = -1; dj <= 1; dj+=2)
          {
            int nj = j + dj;
            int nk = k + 1;
            if (nj > 0 && nj < n)
            {
              chmax(dp[nk][i + 1][nj], dp[k][i][j] + a[i] * nj);
            }
          }
        }
      }
    }

    long long ans = max(dp[2][n - 1][2], dp[1][n - 1][1]);
    int j, k;
    if (dp[2][n - 1][2] >= dp[1][n - 1][1])
    {
      j = 2;
      k = 2;
    }
    else
    {
      j = 1;
      k = 1;
    }

    int i = n - 1;
    vector<int> res;
    res.push_back(0);
    while (i > 0)
    {
      res.push_back(j);
      int nxtj = -1, nxtk = -1;
      for (auto [dj, dk] : vector<pair<int, int>>{{-2, 0}, {-1, 1}, {0, 0}, {1, 1}, {2, 0}})
      {
        if (j - dj >= 0 && j - dj < n && k - dk >= 0 && dp[k][i][j] == dp[k - dk][i - 1][j - dj] + a[i - 1] * j)
        {
          nxtj = j - dj;
          nxtk = k - dk;
        }
      }
      assert(nxtj != -1);
      j = nxtj;
      k = nxtk;
      i--;
    }
    res.push_back(0);
    reverse(res.begin(), res.end());

    cout << ans << endl;
    vector<int> c(n);
    for (int i = 0; i < n; ++i)
    {
      if (res[i] == res[i + 1])
        c[i] = 0;
      else if (res[i] == res[i + 1] + 2)
        c[i] = 1;
      else if (res[i] + 2 == res[i + 1])
        c[i] = 2;
      else
        c[i] = 3;
    }

    int now = distance(c.begin(), find(c.begin(), c.end(), 3));
    int dir = (res[now] < res[now + 1]) ? 1 : -1;
    vector<int> ans_list;
    ans_list.push_back(now);
    vector<int> seen(n, 0);
    seen[now] = 1;
    while (true)
    {
      if (seen[now] == 1)
      {
        now += dir;
        continue;
      }
      if (c[now] == 0)
      {
        ans_list.push_back(now);
        seen[now] = 1;
        now += dir;
      }
      if (c[now] == 1)
      {
        if (dir == 1)
        {
          seen[now] = 1;
          ans_list.push_back(now);
          dir = -dir;
        }
        else
        {
          now += dir;
        }
      }
      if (c[now] == 2)
      {
        if (dir == -1)
        {
          seen[now] = 1;
          ans_list.push_back(now);
          dir = -dir;
        }
        else
        {
          now += dir;
        }
      }
      if (c[now] == 3)
      {
        if (ans_list.size() == n - 1)
        {
          ans_list.push_back(now);
          break;
        }
        else
        {
          now += dir;
        }
      }
    }

    for (int i = 0; i < ans_list.size(); ++i)
      cout << ans_list[i] + 1 << " ";
    cout << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 2010
memory_kb: 281920
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;

template <typename T>
bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}

vector<pair<int, int>> djk = {{-2, 0}, {-1, 1}, {0, 0}, {1, 1}, {2, 0}};
int main()
{
  while (true)
  {
    int n;
    cin >> n;
    if (n == 0)
      break;

    vector<long long> a(n);
    for (int i = 0; i < n - 1; ++i)
      cin >> a[i];

    const long long inf = 1LL << 50;
    vector<vector<vector<long long>>> dp(3, vector<vector<long long>>(n, vector<long long>(n, -inf)));
    dp[0][0][0] = 0;

    for (int i = 0; i < n - 1; ++i)
    {
      for (int j = 0; j < n; ++j)
      {
        if (j > 2 * i || j > 2 * (n - i))
          continue;
        for (int k = 0; k < 3; ++k)
        {
          if (dp[k][i][j] == -inf)
            continue;
          for (auto [dj, dk] : djk)
          {
            int nj = j + dj;
            int nk = k + dk;
            if (nj > 0 && nj < n && nk < 3)
            {
              chmax(dp[nk][i + 1][nj], dp[k][i][j] + a[i] * nj);
            }
          }
        }
      }
    }

    long long ans = max(dp[2][n - 1][2], dp[1][n - 1][1]);
    int j, k;
    if (dp[2][n - 1][2] >= dp[1][n - 1][1])
    {
      j = 2;
      k = 2;
    }
    else
    {
      j = 1;
      k = 1;
    }

    int i = n - 1;
    vector<int> res;
    res.push_back(0);
    while (i > 0)
    {
      res.push_back(j);
      int nxtj = -1, nxtk = -1;
      for (auto [dj, dk] : vector<pair<int, int>>{{-2, 0}, {-1, 1}, {0, 0}, {1, 1}, {2, 0}})
      {
        if (j - dj >= 0 && j - dj < n && k - dk >= 0 && dp[k][i][j] == dp[k - dk][i - 1][j - dj] + a[i - 1] * j)
        {
          nxtj = j - dj;
          nxtk = k - dk;
        }
      }
      assert(nxtj != -1);
      j = nxtj;
      k = nxtk;
      i--;
    }
    res.push_back(0);
    reverse(res.begin(), res.end());

    cout << ans << endl;
    vector<int> c(n);
    for (int i = 0; i < n; ++i)
    {
      if (res[i] == res[i + 1])
        c[i] = 0;
      else if (res[i] == res[i + 1] + 2)
        c[i] = 1;
      else if (res[i] + 2 == res[i + 1])
        c[i] = 2;
      else
        c[i] = 3;
    }

    int now = distance(c.begin(), find(c.begin(), c.end(), 3));
    int dir = (res[now] < res[now + 1]) ? 1 : -1;
    vector<int> ans_list;
    ans_list.push_back(now);
    vector<int> seen(n, 0);
    seen[now] = 1;
    while (true)
    {
      if (seen[now] == 1)
      {
        now += dir;
        continue;
      }
      if (c[now] == 0)
      {
        ans_list.push_back(now);
        seen[now] = 1;
        now += dir;
      }
      if (c[now] == 1)
      {
        if (dir == 1)
        {
          seen[now] = 1;
          ans_list.push_back(now);
          dir = -dir;
        }
        else
        {
          now += dir;
        }
      }
      if (c[now] == 2)
      {
        if (dir == -1)
        {
          seen[now] = 1;
          ans_list.push_back(now);
          dir = -dir;
        }
        else
        {
          now += dir;
        }
      }
      if (c[now] == 3)
      {
        if (ans_list.size() == n - 1)
        {
          ans_list.push_back(now);
          break;
        }
        else
        {
          now += dir;
        }
      }
    }

    for (int i = 0; i < ans_list.size(); ++i)
      cout << ans_list[i] + 1 << " ";
    cout << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 2080
memory_kb: 281892
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vvec = vector<vector<T>>;
template<class T> using vvvec = vector<vector<vector<T>>>;
const ll LINF = 1e18;

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    while(1){
        int N; cin >> N;
        if(N == 0) break;
        vector<ll> C(N);
        for(int i = 0; i < N - 1; i++) cin >> C[i];
        C[N - 1] = 0;

        int M = N + 10;
        vvvec<ll> dp(N + 1, vvec<ll>(M + 1, vector<ll>(3, -LINF)));
        dp[0][0][0] = 0;

        for(int i = 0; i < N; i++){
            for(int j = 0; j <= M; j++){
                for(int k = 0; k < 3; k++){
                    auto e = dp[i][j][k];
                    if(e == -LINF) continue;
                    for(int r = -2; r <= 2; r++){
                        int num = j + r;
                        if(num < 0 || M < num) continue;
                        if(abs(r) == 1 && k == 2) continue;
                        if(i != N - 1 && num == 0) continue;
                        auto &f = dp[i + 1][num][k + (abs(r) == 1)];
                        f = max(f, e + C[i] * num);
                    }
                }
            }
        }

        cout << dp[N][0][2] << endl;

        vector<int> edge(N + 1);
        tuple<int, int, int> par = make_tuple(N, 0, 2);
        for(int i = N; i > 0; i--){
            auto [a, b, c] = par;
            for(int r = 2; r >= -2; r--){
                int num = b + r;
                if(num < 0 || M < num) continue;
                if(abs(r) == 1 && c == 0) continue;
                if(a - 1 != 0 && num == 0) continue;
                auto f = dp[a - 1][num][c - int(abs(r) == 1)];
                if(f == dp[a][b][c] - C[a - 1] * b){
                    par = make_tuple(a - 1, num, c - int(abs(r) == 1));
                    break;
                }
            }
            assert(get<0>(par) == i - 1);
            if(i - 2 >= 0) edge[i - 1] = get<1>(par);
        }

        //for(int i = 0; i < N + 1; i++) cout << edge[i] << ' ';
        //cout << endl;

        int S = -1, T = -1;
        for(int i = 0; i < N; i++){
            if((edge[i] + edge[i + 1]) % 2 == 1){
                if(S == -1) S = i;
                else if(T == -1) T = i;
                else assert(false);
            }
        }
        //cout << S << " " << T << endl;

        int cur = S, prev = -1, dir = -1; //(0: Left, 1: Right)
        vector<int> ans;
        vector<bool> used(N);
        //used[S] = 1;
        while(1){
            //.2.4.3.2.
            //4.2.1.5.3
            bool same = (edge[cur] == edge[cur + 1]);
            if(same && edge[cur] == 0){
                ans.emplace_back(cur);
                break;
            }
            if((same && dir == 0) || edge[cur] > edge[cur + 1]){ // Go Left
                if(dir != 0){
                    used[cur] = 1;
                    ans.emplace_back(cur);
                }
                else if(!used[cur] && edge[cur] + edge[cur + 1] == 1){
                    used[cur] = 1;
                    ans.emplace_back(cur);
                }
                edge[cur]--;
                cur--;
                dir = 0;
            }
            else if((same && dir == 1) || edge[cur] < edge[cur + 1]){ // Go right
                if(dir != 1){
                    used[cur] = 1;
                    ans.emplace_back(cur);
                }
                else if(!used[cur] && edge[cur] + edge[cur + 1] == 1){
                    used[cur] = 1;
                    ans.emplace_back(cur);
                }
                edge[cur + 1]--;
                cur++;
                dir = 1;
            }
            else assert(false);
        }

        assert((int)ans.size() == N);
        for(int i = 0; i < N; i++){
            cout << ans[i] + 1;
            if(i != N - 1) cout << ' ';
        }
        cout << endl;
    }
}
accuracy: 1
time_ms: 6720
memory_kb: 492368
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;

template<class T>
bool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}

template<class T>
bool chmax(T &a, const T &b) {if (a < b) {a = b; return 1;} return 0;}

using ll = long long;
using ld = long double;

bool is_end = false;

const int nmax = 3010;
ll INF =  1e18;

ll dp[nmax][nmax][4];
pair<int, int> prev_cs[nmax][nmax][4];

void init(int n)
{
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < min(2 * n, nmax); ++j)
        {
            for (int s = 0; s < 4; ++s)
            {
                dp[i][j][s] = -INF;
                prev_cs[i][j][s] = {-1, -1};
            }
        }
    }
    
    return;
}

void calc()
{
    ll N; cin >> N;
    
    if (N == 0)
    {
        is_end = true;
        return;
    }
    
    vector<ll> C(N+1, 0);
    for (int i = 1; i < N; ++i) cin >> C[i];
    
    dp[0][0][0] = 0;
    for (int i = 1; i < N; ++i)
    {
        for (int c = 0; c <= min(2 * i, nmax); ++c)
        {
            for (int s = 0; s < 3; ++s)
            {
                ll base = c * C[i];
                ll var = -INF;
                
                for (int d = c - 2; d <= c + 2; d += 2)
                {
                    if (d < 0) continue;
                    if (d == 0 && i > 1) continue;
                    if (chmax(var, dp[i-1][d][s])) prev_cs[i][c][s] = {d, s};
                }
                
                for (int d = c - 1; d <= c + 1; d += 2)
                {
                    if (d < 0) continue;
                    if (d == 0 && i > 1) continue;
                    if (s - 1 < 0) continue;
                    if (chmax(var, dp[i-1][d][s-1])) prev_cs[i][c][s] = {d, s-1};
                }
                
                dp[i][c][s] = base + var;
            }
        }
    }
    
    {
        int i = N;
        for (int c = 0; c <= 2 * i; ++c)
        {
            for (int s = 0; s < 3; ++s)
            {
                ll base = c * C[i];
                ll var = -INF;
                
                for (int d = c - 2; d <= c + 2; d += 2)
                {
                    if (d < 0) continue;
                    if (d == 0 && i > 1) continue;
                    if (chmax(var, dp[i-1][d][s])) prev_cs[i][c][s] = {d, s};
                }
                
                for (int d = c - 1; d <= c + 1; d += 2)
                {
                    if (d < 0) continue;
                    if (d == 0 && i > 1) continue;
                    if (s - 1 < 0) continue;
                    if (chmax(var, dp[i-1][d][s-1])) prev_cs[i][c][s] = {d, s-1};
                }
                
                dp[i][c][s] = base + var;
            }
        }
    }
    
    ll res = dp[N][0][2];
    
    vector<ll> cnt(N+1, 0);
    
    {
        int i = N, c = 0, s = 2;
        while (i >= 0)
        {
            cnt[i] = c;
            auto [cc, ss] = prev_cs[i][c][s];
            i -= 1;
            c = cc, s = ss;
        }
    }
    
    ll left = -1, right = -1;
    for (int i = 0; i < N; ++i)
    {
        if ((cnt[i] + cnt[i+1]) % 2 == 1)
        {
            if (left == -1) left = i;
            else right = i;
        }
    }
    
    vector<ll> rank(N, 0);
    vector<bool> done(N, false);
    
    rank[left] = 0, rank[right] = INF;
    done[left] = done[right] = true;
    
    ll now = left;
    ll dir = 0;
    ll move = 0;
    if (cnt[now] > cnt[now+1]) dir = -1;
    if (cnt[now] < cnt[now+1]) dir = 1;
    
    while (1)
    {
        if (cnt[now] == 0 && cnt[now+1] == 0) break;
        
        ll ndir = 0;
        if (cnt[now] > cnt[now+1]) ndir = -1;
        else if (cnt[now] < cnt[now+1]) ndir = 1;
        else ndir = dir;
        
        if (dir == ndir)
        {
            if (!done[now]) rank[now] = move;
            
            cnt[max(now, now + ndir)] -= 1;
            now += ndir;
        }
        else
        {
            rank[now] = move;
            done[now] = true;
            
            cnt[max(now, now + ndir)] -= 1;
            now += ndir;
        }
        
        dir = ndir;
        move++;
    }
    assert(now == right);
    
    vector<pair<ll, ll>> ord(N);
    for (int i = 0; i < N; ++i)
    {
        ord[i] = {rank[i], i+1};
    }
    sort(ord.begin(), ord.end());
    
    cout << res << endl;
    for (int i = 0; i < N; ++i)
    {
        cout << ord[i].second << " ";
    }
    cout << endl;
    
    init(N + 10);
    
    return;
}

int main()
{
    init(nmax);
    while (!is_end)
    {
        calc();
    }
    
    return 0;
}
accuracy: 1
time_ms: 2790
memory_kb: 569836
final_rank: 8