Problem:
G: Round Tour
問題
\(N\) 頂点の有向グラフがあり、頂点の番号は \(1,2,...,N\) です。
はじめ、\(i < j \) を満たす全ての頂点の組 \((i, j)\) について
    \(i\) から \(j\) にコスト \(E_{ij}\) の有向辺が張られています。
頂点 \(N\) にはスイッチがあり、スイッチを押すと全ての有向辺の向きが反転してコストが変化します。
    具体的には、元の有向辺が削除され、\(i > j\) を満たす全ての頂点の組 \((i, j)\) について \(i\) から \(j\) にコスト \(E_{ij}\) の有向辺が張られます。
TUAT君は頂点 \(1\) からスタートして、辺を経由して頂点 \(N\) にあるスイッチを押した後、再び頂点 \(1\) に戻ります。
    しかし、ただ往復するだけでは面白くないため、TUAT君は全ての頂点を \(1\) 回以上通り、辺のコストの和が最小となるような往復経路を通ることにしました。
最小のコストとそのときの往復経路を \(1\) つ答えてください。
入力形式
N
E_{11} E_{12} ... E_{1N}
E_{21} E_{22} ... E_{2N}
︙
E_{N1} E_{N2} ... E_{NN}
制約
\(2 \leq N \leq 2000\)
\(1 \leq E_{i,j} \leq 10^5 \ (1\leq i \leq N, 1\leq j \leq N, i ≠ j)\)
\(E_{ii} = 0 \ (1 \leq i \leq N)\)
入力はすべて整数
出力形式
以下の形式で答えを出力せよ。
C_{min}
M
v_1
v_2
...
v_M
\(C_{min}\) は最小のコスト、\(M\) は経路の頂点数、\(v_i\ (1\leq v_i \leq N,\ v_1=v_M=1)\) は経路上で \(i\) 番目に通る頂点番号を表す。
入力例1
3
0 1 3
3 0 2
2 4 0
出力例1
5
4
1 2 3 1
入力例2
4
0 1 7 8
4 0 2 9
8 3 0 2
10 5 1 0
出力例2
13
7
1 2 3 4 3 2 1

Submitted solutions:

code_snippet: // AOJ #3235
// Round Tour 2025.2.13

#include <bits/stdc++.h>
using namespace std;
using ll = long long; 
const ll INF = 1e15;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {  // 整数の入力
	int n = 0, c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Coutll(ll n) { // 整数の表示（出力）
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}
void Cout(int n) { // 整数の表示（出力）
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
//	pc('\n');
}

struct Prev {
    int pa, pb;
    int option;
    int newv;
    bool valid;
};
 
int main(){
    int N = Cin();
    vector<vector<ll>> E(N+1, vector<ll>(N+1, 0));
    for (int i = 1; i <= N; i++){
        for (int j = 1; j <= N; j++) E[i][j] = Cin();
    }
 
    vector<vector<ll>> dp(N+1, vector<ll>(N+1, INF));
    vector<vector<Prev>> par(N+1, vector<Prev>(N+1, {0,0,0,0,false}));
 
    dp[1][1] = 0;
    par[1][1] = {0,0,0,1,true};
 
    for (int k = 1; k < N; k++){
        int nxt = k + 1;
        for (int a = 1; a <= k; a++){
            for (int b = 1; b <= k; b++){
                if(max(a, b) != k) continue;
                if(dp[a][b] == INF) continue;
                ll cur = dp[a][b];
 
                ll nc = cur + E[a][nxt];
                if(nc < dp[nxt][b]){
                    dp[nxt][b] = nc;
                    par[nxt][b] = {a, b, 1, nxt, true};
                }
 
                nc = cur + E[nxt][b];
                if(nc < dp[a][nxt]){
                    dp[a][nxt] = nc;
                    par[a][nxt] = {a, b, 2, nxt, true};
                }
 
                nc = cur + E[a][nxt] + E[nxt][b];
                if(nc < dp[nxt][nxt]){
                    dp[nxt][nxt] = nc;
                    par[nxt][nxt] = {a, b, 3, nxt, true};
                }
            }
        }
    }
 
    for (int a = 1; a < N; a++){
        if(dp[a][N] < INF){
            ll nc = dp[a][N] + E[a][N];
            if(nc < dp[N][N]){
                dp[N][N] = nc;
                par[N][N] = {a, N, 4, N, true};
            }
        }
    }
    for (int b = 1; b < N; b++){
        if(dp[N][b] < INF){
            ll nc = dp[N][b] + E[N][b];
            if(nc < dp[N][N]){
                dp[N][N] = nc;
                par[N][N] = {N, b, 4, N, true};
            }
        }
    }
    ll ans = dp[N][N];
    vector<tuple<int,int,int,int>> trans;
    int curA = N, curB = N;
    while(!(curA == 1 && curB == 1)){
        Prev p = par[curA][curB];
        trans.push_back({curA, curB, p.option, p.newv});
        int nA = p.pa, nB = p.pb;
        curA = nA; curB = nB;
    }
    reverse(trans.begin(), trans.end());
 
    vector<int> pathF, pathB;
    pathF.push_back(1);
    pathB.push_back(1);
 
    for(auto &t : trans){
        int a, b, op, nv;
        tie(a, b, op, nv) = t;
        if(op == 1) pathF.push_back(nv);
        else if(op == 2) pathB.push_back(nv);
        else if(op == 3){
            pathF.push_back(nv);
            pathB.push_back(nv);
        } else if(op == 4){
            if(a != N) pathF.push_back(nv);
            else if(b != N) pathB.push_back(nv);
        }
    }
 
    if(pathF.back() != N) pathF.push_back(N);
    if(pathB.back() != N) pathB.push_back(N);
 
    vector<int> tour;
    for (int v : pathF) tour.push_back(v);
    vector<int> revB = pathB;
    reverse(revB.begin(), revB.end());
    for (int i = 1; i < (int)revB.size(); i++) tour.push_back(revB[i]);
 
    Coutll(ans);
    Cout(tour.size()), pc('\n');
    Cout(tour[0]);
    for (int i = 1; i < (int)tour.size(); i++) pc(' '), Cout(tour[i]);
    pc('\n');
    return 0;
}
accuracy: 1
time_ms: 1460
memory_kb: 144236
final_rank: 18

code_snippet: // AOJ #3235
// Round Tour 2025.2.13

#include <bits/stdc++.h>
using namespace std;
using ll = long long; 
const ll INF = 1e15;
 
struct Prev {
    int pa, pb;
    int option;
    int newv;
    bool valid;
};
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    int N;
    cin >> N;
    vector<vector<ll>> E(N+1, vector<ll>(N+1, 0));
    for (int i = 1; i <= N; i++){
        for (int j = 1; j <= N; j++) cin >> E[i][j];
    }
 
    vector<vector<ll>> dp(N+1, vector<ll>(N+1, INF));
    vector<vector<Prev>> par(N+1, vector<Prev>(N+1, {0,0,0,0,false}));
 
    dp[1][1] = 0;
    par[1][1] = {0,0,0,1,true};
 
    for (int k = 1; k < N; k++){
        int nxt = k + 1;
        for (int a = 1; a <= k; a++){
            for (int b = 1; b <= k; b++){
                if(max(a, b) != k) continue;
                if(dp[a][b] == INF) continue;
                ll cur = dp[a][b];
 
                ll nc = cur + E[a][nxt];
                if(nc < dp[nxt][b]){
                    dp[nxt][b] = nc;
                    par[nxt][b] = {a, b, 1, nxt, true};
                }
 
                nc = cur + E[nxt][b];
                if(nc < dp[a][nxt]){
                    dp[a][nxt] = nc;
                    par[a][nxt] = {a, b, 2, nxt, true};
                }
 
                nc = cur + E[a][nxt] + E[nxt][b];
                if(nc < dp[nxt][nxt]){
                    dp[nxt][nxt] = nc;
                    par[nxt][nxt] = {a, b, 3, nxt, true};
                }
            }
        }
    }
 
    for (int a = 1; a < N; a++){
        if(dp[a][N] < INF){
            ll nc = dp[a][N] + E[a][N];
            if(nc < dp[N][N]){
                dp[N][N] = nc;
                par[N][N] = {a, N, 4, N, true};
            }
        }
    }
    for (int b = 1; b < N; b++){
        if(dp[N][b] < INF){
            ll nc = dp[N][b] + E[N][b];
            if(nc < dp[N][N]){
                dp[N][N] = nc;
                par[N][N] = {N, b, 4, N, true};
            }
        }
    }
    ll ans = dp[N][N];
    vector<tuple<int,int,int,int>> trans;
    int curA = N, curB = N;
    while(!(curA == 1 && curB == 1)){
        Prev p = par[curA][curB];
        trans.push_back({curA, curB, p.option, p.newv});
        int nA = p.pa, nB = p.pb;
        curA = nA; curB = nB;
    }
    reverse(trans.begin(), trans.end());
 
    vector<int> pathF, pathB;
    pathF.push_back(1);
    pathB.push_back(1);
 
    for(auto &t : trans){
        int a, b, op, nv;
        tie(a, b, op, nv) = t;
        if(op == 1) pathF.push_back(nv);
        else if(op == 2) pathB.push_back(nv);
        else if(op == 3){
            pathF.push_back(nv);
            pathB.push_back(nv);
        } else if(op == 4){
            if(a != N) pathF.push_back(nv);
            else if(b != N) pathB.push_back(nv);
        }
    }
 
    if(pathF.back() != N) pathF.push_back(N);
    if(pathB.back() != N) pathB.push_back(N);
 
    vector<int> tour;
    for (int v : pathF) tour.push_back(v);
    vector<int> revB = pathB;
    reverse(revB.begin(), revB.end());
    for (int i = 1; i < (int)revB.size(); i++)
        tour.push_back(revB[i]);
 
    cout << ans << endl;
    cout << tour.size() << endl;
    for (int i = 0; i < (int)tour.size(); i++){
        cout << tour[i] << (i+1 == tour.size() ? "\n" : " ");
    }
    return 0;
}
accuracy: 1
time_ms: 1670
memory_kb: 144064
final_rank: 19

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 3235.cc: Round Tour
 */

#include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 2000;
const int INF = 1 << 30;

/* typedef */

typedef vector<int> vi;
typedef pair<int,int> pii;

/* global variables */

int es[MAX_N][MAX_N], dp[MAX_N][MAX_N];
pii ps[MAX_N][MAX_N];

/* subroutines */

inline void setmin(int &a, int b) { if (a > b) a = b; }

void check(int n, int i0, int j0, int i1, int j1) { // i0,j0 -> i1,j1
  if (i1 < n && j1 < n) {
    int d1 = dp[i0][j0] + ((i0 < i1) ? es[i0][i1] : es[j1][j0]);
    if (dp[i1][j1] > d1)
      dp[i1][j1] = d1, ps[i1][j1] = pii(i0, j0);
  }
}

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) scanf("%d", es[i] + j);

  for (int i = 0; i < n; i++) fill(dp[i], dp[i] + n, INF);
  dp[0][0] = 0, ps[0][0] = pii(-1, -1);

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      check(n, i, j, i + 1, j);
      check(n, i, j, i, j + 1);
      if (i < j)
	check(n, i, j, j, j);
      else if (i > j)
	check(n, i, j, i, i);
	
      if (i + 1 < n && j + 1 < n) {
	int k = max(i + 1, j + 1);
	check(n, i, j, k, j);
	check(n, i, j, i, k);
      }
    }

  vi v0, v1;
  for (int i = n - 1, j = n - 1; i > 0 || j > 0;) {
    pii p = ps[i][j];
    int i1 = p.first, j1 = p.second;
    if (i1 != i) v0.push_back(i1);
    else v1.push_back(j1);
    i = i1, j = j1;
  }

  reverse(v0.begin(), v0.end());
  vi v;
  for (auto u: v0) v.push_back(u);
  v.push_back(n - 1);
  for (auto u: v1) v.push_back(u);
  
  int m = v.size();
  printf("%d\n%d\n", dp[n - 1][n - 1], m);
  for (int i = 0; i < m; i++)
    printf("%d%c", v[i] + 1, (i + 1 < m) ? ' ' : '\n');

  return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 65476
final_rank: 7

code_snippet: #include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <vector>
#include <numeric>
#include <algorithm>
#include <queue>
#include <string>
#include <random>
#include <array>
#include <climits>

int main() {
	int n; std::cin >> n;
	std::vector<std::vector<int>> cost(n, std::vector<int>(n));
	for (auto& line : cost) {
		for (auto& e : line) {
			std::cin >> e;
		}
	}
	std::vector<std::vector<int>> min_cost(n, std::vector<int>(n, INT_MAX >> 1));
	std::vector<std::vector<std::pair<int, int>>> prev_state(n, std::vector<std::pair<int, int>>(n));
	min_cost[0][0] = 0;
	for (auto i = 1; i < n; ++i) {
		for (auto f = i - 1; f >= 0; --f) {
			if (min_cost[i][i - 1] > min_cost[f][i - 1] + cost[f][i]) {
				min_cost[i][i - 1] = min_cost[f][i - 1] + cost[f][i];
				prev_state[i][i - 1] = std::make_pair(f, i - 1);
			}
			if (min_cost[f][i] > min_cost[f][i - 1] + cost[i][i - 1]) {
				min_cost[f][i] = min_cost[f][i - 1] + cost[i][i - 1];
				prev_state[f][i] = std::make_pair(f, i - 1);
			}
		}
		for (auto b = i - 1; b >= 0; --b) {
			if (min_cost[i - 1][i] > min_cost[i - 1][b] + cost[i][b]) {
				min_cost[i - 1][i] = min_cost[i - 1][b] + cost[i][b];
				prev_state[i - 1][i] = std::make_pair(i - 1, b);
			}
			if (min_cost[i][b] > min_cost[i - 1][b] + cost[i - 1][i]) {
				min_cost[i][b] = min_cost[i - 1][b] + cost[i - 1][i];
				prev_state[i][b] = std::make_pair(i - 1, b);
			}
		}
		if (min_cost[i][i - 1] + cost[i][i - 1] < min_cost[i - 1][i] + cost[i - 1][i]) {
			min_cost[i][i] = min_cost[i][i - 1] + cost[i][i - 1];
			prev_state[i][i] = std::make_pair(i, i - 1);
		}
		else {
			min_cost[i][i] = min_cost[i - 1][i] + cost[i - 1][i];
			prev_state[i][i] = std::make_pair(i - 1, i);
		}
	}
	auto [f, b] = prev_state[n - 1][n - 1];
	std::vector<std::pair<int, int>> path{std::make_pair(f, b)};
	while (f != 0 || b != 0) {
		const auto [i, j] = prev_state[f][b];
		path.emplace_back(i, j);
		f = i;
		b = j;
	}
	std::reverse(path.begin(), path.end());
	std::vector<int> result;
	for (const auto [f, _] : path) {
		if (result.empty() || result.back() != f) {
			result.push_back(f);
		}
	}
	std::reverse(path.begin(), path.end());
	for (const auto [_, b] : path) {
		if (result.back() != b) {
			result.push_back(b);
		}
	}
	std::cout << min_cost[n - 1][n - 1] << '\n';
	std::cout << result.size() << '\n';
	for (auto i = 0; i < result.size(); ++i) {
		if (i > 0) std::cout << ' ';
		std::cout << result[i] + 1;
	}
	std::cout << '\n';
}
accuracy: 1
time_ms: 730
memory_kb: 65908
final_rank: 12

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=(n-1);i>=0;i--)
#define ALL(v) v.begin(),v.end()

template<typename T,typename ...Args>
auto make_vector(T x,int arg,Args ...args){
  if constexpr(sizeof...(args)==0)return vector<T>(arg,x);
  else return vector(arg,make_vector<T>(x,args...));
}
using ll=long long;
const long long LINF=1e18;
template<typename T>
bool chmin(T &a,T b){
  return (a>b&&(a=b,true));
}

int main(){
  int n;cin>>n;
  auto e=make_vector<ll>(0,n,n);
  REP(i,n)REP(j,n)cin>>e[i][j];
  auto dp=make_vector<ll>(LINF,n,n);
  dp[0][0]=0;
  for(int k=1;k<n;k++){
    REP(i,k){
      chmin(dp[k-1][k],dp[k-1][i]+e[i][k]);
      chmin(dp[k][k-1],dp[i][k-1]+e[k][i]);
    }
    REP(i,k+1){
      chmin(dp[i][k],dp[i][k-1]+e[k-1][k]);
      chmin(dp[k][i],dp[k-1][i]+e[k][k-1]);
    }
  }
  cout<<dp[n-1][n-1]<<endl;
  deque<int> que;
  que.push_front(n-1);
  int u=n-1,v=n-1;
  while(u||v){
    if(u>=v){
      if(v>=u-1){
        //元はvの方が大きい
        REP(i,u)if(dp[i][v]+e[u][i]==dp[u][v]){
          u=i;
          que.push_back(u);
        }
      }
      else que.push_back(--u);
    }
    else{
      if(u>=v-1){
        //元はvの方が大きい
        REP(i,v)if(dp[u][i]+e[i][v]==dp[u][v]){
          v=i;
          que.push_front(v);
        }
      }
      else que.push_front(--v);
    }
  }
  cout<<que.size()<<endl;
  for(int p:que)cout<<p+1<<" ";cout<<endl;
}
accuracy: 1
time_ms: 720
memory_kb: 66032
final_rank: 11

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N;
    cin >> N;
    vector<vector<int>> E(N, vector<int>(N));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            cin >> E[i][j];
        }
    }
    vector<vector<int>> dp(N, vector<int>(N, 1e9));
    vector<vector<pair<int, int>>> from(N, vector<pair<int, int>>(N));
    dp[0][0] = 0;

    auto update = [&](int i, int j, int v, pair<int, int> f) {
        if (dp[i][j] > v) {
            dp[i][j] = v;
            from[i][j] = f;
        }
    };

    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            if (i >= j) {
                if (i != j) update(i, i, dp[i][j] + E[i][j], {i, j});
                if (i < N - 1) {
                    update(i+1, j, dp[i][j] + E[i][i+1], {i, j});
                    update(i, i+1, dp[i][j] + E[i+1][j], {i, j});
                }
            }
            if (i <= j) {
                if (i != j) update(j ,j, dp[i][j] + E[i][j], {i, j});
                if (j < N - 1) {
                    update(i, j+1, dp[i][j] + E[j+1][j], {i, j});
                    update(j+1, j, dp[i][j] + E[i][j+1], {i, j});
                }
            }
        }
    }
    vector<int> first, second;
    first.push_back(N - 1);
    second.push_back(N - 1);
    int i = N - 1, j = N - 1;
    while (i > 0 || j > 0) {
        tie(i, j) = from[i][j];
        if (first.back() != i) first.push_back(i);
        if (second.back() != j) second.push_back(j);
    }
    reverse(first.begin(), first.end());
    first.pop_back();
    copy(second.begin(), second.end(), back_inserter(first));
    cout << dp[N-1][N-1] << endl;
    cout << first.size() << endl;
    for (int i = 0; i < first.size(); ++i) cout << first[i] + 1 << (i < first.size() - 1 ? " " : "\n");

}
accuracy: 1
time_ms: 240
memory_kb: 65792
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;

template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

vector<vector<int>> dp(2010,vector<int>(2011,INF));

signed main(){
    int N;cin>>N;
    vector<vector<int>> G(N,vector<int>(N));
    using P=pair<int,int>;
    vector<vector<P>> B(N,vector<P>(N,{-1,-1}));
    cin>>G;

    dp[0][0]=0;

    rep(i,N){
        rep(j,N){
            if(i>=j){
                if(i+1<N){
                    if(chmin(dp[i+1][j],dp[i][j]+G[i][i+1])) B[i+1][j]=P(i,j);
                    if(chmin(dp[i][i+1],dp[i][j]+G[i+1][j])) B[i][i+1]=P(i,j);
                }
                if(chmin(dp[i][i],dp[i][j]+G[i][j])) B[i][i]=P(i,j);
            }

            if(j>=i){
                if(j+1<N){
                    if(chmin(dp[i][j+1],dp[i][j]+G[j+1][j])) B[i][j+1]=P(i,j);
                    if(chmin(dp[j+1][j],dp[i][j]+G[i][j+1])) B[j+1][j]=P(i,j);
                }
                if(chmin(dp[j][j],dp[i][j]+G[i][j])) B[j][j]=P(i,j);
            }
        }
    }


    vector<int> L,R;
    for(int i=N-1,j=N-1;;){
        if(B[i][j].first<0) break;

        auto [ni,nj]=B[i][j];
        if(ni==i) R.push_back(nj);
        if(nj==j) L.push_back(ni);

        i=ni,j=nj;
    }

    vector<int> ans;
    for(int i=(int)L.size()-1;i>=0;i--)  ans.push_back(L[i]+1);
    ans.push_back(N);
    for(int i=0;i<(int)R.size();i++)     ans.push_back(R[i]+1); 

    cout<<dp[N-1][N-1]<<endl;
    cout<<ans.size()<<endl;
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 66172
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={0,1,-1,0,1,1,-1,-1,0};
const ll dx[9]={1,0,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
} 

int main(){
    ll n;cin >> n;
    vvl e(n,vl(n)),f(n,vl(n));
    rep(i,n){
        rep(j,n){
            if(i<j)cin >> e[i][j];
            else cin >> f[j][i];
        }
    }
    vvl dp(n,vl(n,INF));dp[0][0]=0;
    vvpl pre(n,vpl(n));
    rep(i,n){
        rep(j,n){
            if(i<j-1){
                if(chmin(dp[i][j],dp[i][j-1]+f[j-1][j])){
                    pre[i][j]={i,j-1};
                }
            }
            else if(j-1<=i&&i<=j+1){
                for(int k=0;k<i;k++){
                    if(chmin(dp[i][j],dp[k][j]+e[k][i])){
                        pre[i][j]={k,j};
                    }
                }
                for(int k=0;k<j;k++){
                    if(chmin(dp[i][j],dp[i][k]+f[k][j])){
                        pre[i][j]={i,k};
                    }
                }
            }
            else{
                if(chmin(dp[i][j],dp[i-1][j]+e[i-1][i])){
                    pre[i][j]={i-1,j};
                }
            }
        }
    }
    cout << dp[n-1][n-1] << endl;
    vl left,right;
    ll nx=n-1,ny=n-1;
    while(nx+ny){
        if(pre[nx][ny].first!=nx){
            left.push_back(nx);
        }
        else{
            right.push_back(ny);
        }
        auto q=pre[nx][ny];
        nx=q.first,ny=q.second;
    }
    left.push_back(0);right.push_back(0);
    rev(all(left));left.pop_back();
    left.insert(left.end(),all(right));
    cout << left.size() << endl;
    for(auto p:left)cout << p+1 <<" ";cout << endl;
}
accuracy: 1
time_ms: 800
memory_kb: 159864
final_rank: 16

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(x,y) x = min((x),(y))
#define chmax(x,y) x = max((x),(y))
#define popcount(x) __builtin_popcountll(x)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
const int INF = 1e9;
const long long LINF = 1e17;
const int MOD = 1000000007;
//const int MOD = 998244353;
const double PI = 3.14159265358979323846;

int main(){
    int n;
    cin >> n;
    vector<vector<ll>> e(n,vector<ll>(n,0));
    rep(i,n)rep(j,n) cin >> e[i][j];

    vector<vector<ll>> dp(n,vector<ll>(n,LINF));
    vector<vector<P>> prev(n,vector<P>(n,{-1,-1}));
    dp[0][0] = 0;

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if (i+1 < j) {
                dp[i][j] = dp[i][j-1] + e[j][j-1];
                prev[i][j] = make_pair(i,j-1);
            }
            else if (j+1 < i) {
                dp[i][j] = dp[i-1][j] + e[i-1][i];
                prev[i][j] = make_pair(i-1,j);
            }
            else {
                // j-1 <= i && i <= j+1
                for(int k=0;k<i;k++) {
                    if (dp[i][j] > dp[k][j] + e[k][i]) {
                        dp[i][j] = dp[k][j] + e[k][i];
                        prev[i][j] = make_pair(k,j);
                    }
                }
                for(int k=0;k<j;k++) {
                    if (dp[i][j] > dp[i][k] + e[j][k]) {
                        dp[i][j] = dp[i][k] + e[j][k];
                        prev[i][j] = make_pair(i,k);
                    }
                }
            }
        }
    }

    cout << dp[n-1][n-1] << endl;
    vector<int> is,js;
    int i = n-1,j = n-1;
    while (i != -1 || j != -1) {
        is.push_back(i);
        js.push_back(j);
        tie(i,j) = prev[i][j];
    }
    sort(is.begin(),is.end());
    sort(js.begin(),js.end());
    is.erase(unique(is.begin(),is.end()),is.end());
    js.erase(unique(js.begin(),js.end()),js.end());

    is.insert(is.end(),js.rbegin() + 1,js.rend());
    cout << is.size() << endl;
    for(int i:is) cout << i+1 << " ";
    cout << endl;

    return 0;
}
accuracy: 1
time_ms: 760
memory_kb: 97060
final_rank: 15

code_snippet: #include <bits/stdc++.h>
#define inf (long long)(1e9)
using namespace std;

using T = tuple<long long, int, int>;

int n;
vector<int> route;
vector<vector<long long>> e;

long long solve();

int main() {
  cin >> n;
  e.assign(n, vector<long long>(n, 0));
  for (auto &v : e)
    for (auto &p : v) cin >> p;
  cout << solve() << endl;
  cout << route.size() << endl;
  for (int i = 0; i < route.size(); ++i)
    cout << route[i] + 1 << " \n"[i == route.size() - 1];
  return 0;
}

long long solve() {
  vector<vector<T>> dp(n, vector<T>(n, T(inf, -1, -1)));
  dp[0][0] = T(0, -1, -1);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) {
      auto [c, x, y] = dp[i][j];
      int to = max(i, j) + 1;
      if (to == n) continue;
      int go = e[i][to], bak = e[to][j];
      dp[to][j] = min(dp[to][j], T(c + go, i, j));
      dp[i][to] = min(dp[i][to], T(c + bak, i, j));
      dp[to][to] = min(dp[to][to], T(c + go + bak, i, j));
    }
  vector<int> lv, rv;
  int x = n - 1, y = n - 1;
  while (x >= 0) {
    auto [_, tx, ty] = dp[x][y];
    lv.push_back(x), rv.push_back(y);
    x = tx, y = ty;
  }
  reverse(lv.begin(), lv.end());
  reverse(rv.begin(), rv.end());
  lv.erase(unique(lv.begin(), lv.end()), lv.end());
  route = lv;
  while (rv.size()) {
    int now = rv.back();
    if (route.back() != now) route.push_back(now);
    rv.pop_back();
  }
  auto [res, a, b] = dp[n - 1][n - 1];
  return res;
}
accuracy: 1
time_ms: 730
memory_kb: 97088
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N; cin >> N;
    vector<vector<int>> E(N, vector<int> (N));
    rep(i,N)rep(j,N) cin >> E[i][j];

    vector<vector<int>> dp(N, vector<int> (N, 1e9));
    vector<vector<pair<int,int>>> pre(N, vector<pair<int,int>>(N));
    dp[0][0] = 0;
    for(int i = 0; i < N - 1; i++) {
        for(int j = 0; j < N - 1; j++) {
            int k = max(i, j) + 1;
            if(dp[k][j] > dp[i][j] + E[i][k]) {
                dp[k][j] = dp[i][j] + E[i][k];
                pre[k][j] = {i, j};
            }
            if(dp[i][k] > dp[i][j] + E[k][j]) {
                dp[i][k] = dp[i][j] + E[k][j];
                pre[i][k] = {i, j};
            }
            if(dp[k][k] > dp[i][j] + E[i][k] + E[k][j]) {
                dp[k][k] = dp[i][j] + E[i][k] + E[k][j];
                pre[k][k] = {i, j};
            }
        }
    }

    cout << dp[N - 1][N - 1] << endl;

    deque<int> ans; ans.push_back(N - 1);
    int i = N - 1, j = N - 1;
    while(i || j) {
        int ti,tj; tie(ti,tj) = pre[i][j];
        if(ti == i) ans.push_back(j = tj);
        else ans.push_front(i = ti);
    }
    cout << ans.size() << endl;
    for(int a : ans) cout << a + 1 << " "; cout << endl;
}
accuracy: 1
time_ms: 240
memory_kb: 65804
final_rank: 4

code_snippet: #pragma region Macros
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define FOR(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

void Case(int i) { cout << "Case #" << i << ": "; }
int popcount(int x) { return __builtin_popcount(x); }
ll popcount(ll x) { return __builtin_popcountll(x); }
#pragma endregion Macros

int main() {
    int N;
    cin >> N;
    vector E(N+1, vector<ll>(N+1));
    REP(i, N) REP(j, N) cin >> E[i][j];
    vector dp(N+1, vector<ll>(N+1, LLINF));
    vector pre(N+1, vector<pair<int, int>>(N+1, {-1, -1}));
    dp[0][0] = 0;
    REP(i, N) REP(j, N) {
        int to = max(i, j) + 1;
        if(chmin(dp[to][j], dp[i][j] + E[i][to])) pre[to][j] = {i, j};
        if(chmin(dp[i][to], dp[i][j] + E[to][j])) pre[i][to] = {i, j};
        if(chmin(dp[to][to], dp[i][j] + E[i][to] + E[to][j])) pre[to][to] = {i, j};
    }
    
    vector<ll> a, b;
    pair<int, int> now(N-1, N-1);
    a.push_back(N-1); b.push_back(N-1);
    while(1) {
        auto [i, j] = now;
        auto [pi, pj] = pre[i][j];
        if(pi == -1 and pj == -1) break;
        if(i != pi) a.push_back(pi);
        if(j != pj) b.push_back(pj);
        now = {pi, pj};
    }
    reverse(ALL(a));
    vector<int> v;
    for(int u : a) v.push_back(u);
    FOR(i, 1, b.size()) v.push_back(b[i]);

    cout << dp[N-1][N-1] << "\n";
    cout << v.size() << "\n";
    REP(i, v.size()) cout << v[i]+1 << " \n"[i+1 == v.size()]; 
}
accuracy: 1
time_ms: 260
memory_kb: 97180
final_rank: 10

code_snippet: #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T, typename V>
void ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); }
template <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); }
template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; }
template <typename T> bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; }
int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> vector<T> sort_unique(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <typename T> int arglb(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); }
template <typename T> int argub(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T, size_t sz> ostream &operator<<(ostream &os, const array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
#if __cplusplus >= 201703L
template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }
#endif
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef HITONANODE_LOCAL
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl
#define dbgif(cond, x) ((cond) ? cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl : cerr)
#else
#define dbg(x) (x)
#define dbgif(cond, x) 0
#endif

int main() {
    int N;
    cin >> N;
    vector E(N, vector<int>(N));
    cin >> E;
    constexpr lint INF = 1LL << 60;
    vector dp(N, vector<lint>(N, INF));
    vector prv(N, vector<pint>(N, {-1, -1}));
    dp[0][0] = 0;
    REP(i, N) REP(j, N) {
        auto trans = [&](int ni, int nj, lint w) {
            if (chmin(dp[ni][nj], dp[i][j] + w)) {
                prv[ni][nj] = {i, j};
            }
        };
        if (i + 1 < N) trans(i + 1, j, E[i][i + 1]);
        if (j + 1 < N) trans(i, j + 1, E[j + 1][j]);
        if (i == j or i + 1 == j) {
            FOR(k, i + 1, N) {
                trans(k, j, E[i][k]);
            }
        }
        if (i == j or i == j + 1) {
            FOR(k, j + 1, N) {
                trans(i, k, E[k][j]);
            }
        }
    }
    deque<int> ret;
    int i = N - 1, j = N - 1;
    while (i >= 0 and j >= 0) {
        auto [ni, nj] = prv[i][j];
        if (i != ni) ret.push_front(i);
        if (j != nj and j != N - 1) ret.push_back(j);
        tie(i, j) = prv[i][j];
    }
    cout << dp[N - 1][N - 1] << '\n';
    cout << ret.size() << '\n';
    for (auto x : ret) cout << x + 1 << ' ';
    cout << endl;
    lint tmp = 0;
    int pr = ret.front();
    for (auto i : ret) tmp += E[pr][i], pr = i;
    assert(tmp == dp.back().back());
}
accuracy: 1
time_ms: 300
memory_kb: 81340
final_rank: 8

code_snippet: #include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <numeric>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <functional>
#include <limits>
#include <cassert>
#include <bitset>
#include <chrono>
#include <random>
#include <memory>
 
/* macro */
 
#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n-1)); i >= (int)(a); i--)
#define Rep(i, a, n) for (i64 i = (i64)(a); i< (i64)(n); i++)
#define RRep(i, a, n) for (i64 i = ((i64)(n-i64(1))); i>=(i64)(a); i--)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define Bit(n) (1LL<<(n))
 
/* macro end */
 
/* template */
 
namespace ebi {
 
#ifdef LOCAL
#define debug(...)                                                    \
    std::cerr << "LINE: " << __LINE__ << "  [" << #__VA_ARGS__ << "]:", \
        debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
 
void debug_out() { std::cerr << std::endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
    std::cerr << " " << h;
    if (sizeof...(t) > 0) std::cout << " :";
    debug_out(t...);
}
 
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
    return os << pa.first << " " << pa.second;
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
    for (std::size_t i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : " ");
    return os;
}
 
using size_t = std::size_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
 
template<class T> void fill(std::vector<T> &v) { for(T &e: v) std::cin >> e; }
 
template <class T>
inline bool chmin(T &a, T b) {
    if (a > b){
        a = b;
        return true;
    }
    return false;
}
 
template <class T>
inline bool chmax(T &a, T b) {
    if (a < b){
        a = b;
        return true;
    }
    return false;
}
 
template <class T>
T gcd(T a, T b){
    if( b==0 ) return a;
    else return gcd(b, a%b);
}
 
template <class T>
T lcm(T a, T b) {
    return (a*b)/gcd(a,b);
}
 
template<class T>
T Pow(T x, i64 n) {
    T res = 1;
    while(n>0){
        if(n&1) res = res *x;
        x = x*x;
        n>>=1;
    }
    return res;
}
 
template<class T>
T scan() {
    T val;
    std::cin >> val;
    return val;
}
 
constexpr i64 LNF = std::numeric_limits<i64>::max()/4;
 
constexpr int INF = std::numeric_limits<int>::max()/2;
 
const long double PI = acos(-1);
 
const std::vector<int> dy = {1,0,-1,0,1,1,-1,-1};
const std::vector<int> dx = {0,1,0,-1,1,-1,1,-1};
 
}
 
/* template end */

namespace ebi {

using ld = long double;

void main_() {
    int n;
    std::cin >> n;
    std::vector e(n, std::vector<int>(n));
    rep(i,0,n) {
        rep(j,0,n) {
            std::cin >> e[i][j];
        }
    }
    std::vector dp(n, std::vector<int>(n, INF));
    std::vector prev(n, std::vector<std::pair<int,int>>(n, {-1, -1}));
    dp[0][0] = 0;
    rep(i,0,n) {
        rep(j,0,n) {
            int l = -1, r = -1;
            if(i < j-1) {
                if(chmin(dp[i][j], dp[i][j-1] + e[j][j-1])) {
                    l = i;
                    r = j-1;
                }
            }
            else if(j - 1 <= i && i <= j+1) {
                rep(k,0,i) {
                    if(chmin(dp[i][j], dp[k][j] + e[k][i])) {
                        l = k;
                        r = j;
                    }
                }
                rep(k,0,j) {
                    if(chmin(dp[i][j], dp[i][k] + e[j][k])) {
                        l = i;
                        r = k;
                    }
                }
            }
            else {
                if(chmin(dp[i][j], dp[i-1][j] + e[i-1][i])) {
                    l = i-1;
                    r = j;
                }
            }
            prev[i][j] = {l, r};
        }
    }
    std::cout << dp[n-1][n-1] << '\n';
    std::vector<int> a,b;
    int g = n-1, r = n-1;
    while(!(g < 0 && r < 0)) {
        a.emplace_back(g+1);
        b.emplace_back(r+1);
        auto [ng, nr] = prev[g][r];
        g = ng;
        r = nr;
    }
    std::reverse(all(a));
    a.erase(std::unique(all(a)), a.end());
    std::reverse(all(b));
    b.erase(std::unique(all(b)), b.end());
    b.pop_back();
    std::reverse(all(b));
    a.insert(a.end(), b.begin(), b.end());
    std::cout << a.size() << '\n';
    for(auto val: a) {
        std::cout << val << " ";
    }
    std::cout << '\n';
}

}

 
int main() {
    std::cout << std::fixed << std::setprecision(15);
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int t = 1;
    // std::cin >> t;
    while(t--) {
        ebi::main_();
    }
    return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 65876
final_rank: 6

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

using ll = long long int;
using ull = unsigned long long int;
using P = pair<ll, ll>;
using P3 = pair<int,P>;
using PP = pair<P, P>;
constexpr int INF32 = 1 << 30;
constexpr ll INF64 = 1LL << 60;
// constexpr ll MOD = 1000000007;
constexpr ll MOD = 998244353;
constexpr int di[] = {0, 1, 0, -1};
constexpr int dj[] = {1, 0, -1, 0};
constexpr int di8[] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dj8[] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr double EPS = 1e-9;
const double PI = acos(-1);

#define ALL(v) (v).begin(),(v).end()
#define REP(i,n) for(int i=0,i_len=n; i<i_len; ++i)

template<typename T1,typename T2> bool chmax(T1 &a, T2 b) { if (a<b) { a=b; return 1; } return 0; }
template<typename T1,typename T2> bool chmin(T1 &a, T2 b) { if (b<a) { a=b; return 1; } return 0; }

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<vector<int> > e(n, vector<int>(n)), dp(n, vector<int>(n, INF32));
    vector<vector<P> > rev(n, vector<P>(n));
    REP(i,n) REP(j,n) cin >> e[i][j];
    dp[0][0] = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i+1<n){
                if(chmin(dp[i+1][j], dp[i][j] + e[i][i+1])){
                    rev[i+1][j] = P(i,j);
                }
                if(j<i){
                    if(chmin(dp[i][i+1], dp[i][j] + e[i+1][j])){
                        rev[i][i+1] = P(i,j);
                    }
                }

            }
            if(j+1<n){
                if(chmin(dp[i][j+1], dp[i][j] + e[j+1][j])){
                    rev[i][j+1] = P(i,j);
                }
                if(i<j) if(chmin(dp[j+1][j], dp[i][j] + e[i][j+1])){
                    rev[j+1][j] = P(i,j);
                }
            }
        }
    }
    cout << dp[n-1][n-1] << endl;
    deque<int> ans;
    ans.push_back(n-1);
    int l = n-1, r = n-1;
    while(l > 0 || r > 0){
        int nl = rev[l][r].first, nr = rev[l][r].second;
        if(nl == l){
            ans.push_back(nr);
            r = nr;
        }else{
            ans.push_front(nl);
            l = nl;
        }
    }
    cout << ans.size() << endl;
    for(auto itr = ans.begin(); itr!=ans.end(); itr++){
        if(itr != ans.begin()) cout << " ";
        cout << *itr+1;
    }
    cout << endl;
    return 0;
}
accuracy: 1
time_ms: 260
memory_kb: 97068
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define inf (long long)(1e9)
using namespace std;

using T = tuple<long long, int, int>;

int n;
vector<int> route;
vector<vector<long long>> e;

long long solve();

int main() {
  cin >> n;
  e.assign(n, vector<long long>(n, 0));
  for (auto &v : e)
    for (auto &p : v) cin >> p;
  cout << solve() << endl;
  cout << route.size() << endl;
  for (int i = 0; i < route.size(); ++i)
    cout << route[i] + 1 << " \n"[i == route.size() - 1];
  return 0;
}

long long solve() {
  vector<vector<T>> dp(n, vector<T>(n, T(inf, -1, -1)));
  dp[0][0] = T(0, -1, -1);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) {
      auto [c, x, y] = dp[i][j];
      int to = max(i, j) + 1;
      if (to == n) continue;
      int go = e[i][to], bak = e[to][j];
      dp[to][j] = min(dp[to][j], T(c + go, i, j));
      dp[i][to] = min(dp[i][to], T(c + bak, i, j));
      dp[to][to] = min(dp[to][to], T(c + go + bak, i, j));
    }
  vector<int> lv, rv;
  int x = n - 1, y = n - 1;
  while (x >= 0) {
    auto [_, tx, ty] = dp[x][y];
    lv.push_back(x), rv.push_back(y);
    x = tx, y = ty;
  }
  reverse(lv.begin(), lv.end());
  reverse(rv.begin(), rv.end());
  for (auto p : lv)
    if (!route.size() || route.back() != p) route.push_back(p);
  while (rv.size()) {
    int now = rv.back();
    if (route.back() != now) route.push_back(now);
    rv.pop_back();
  }
  auto [res, a, b] = dp[n - 1][n - 1];
  return res;
}
accuracy: 1
time_ms: 730
memory_kb: 96928
final_rank: 13

code_snippet: // これでACになるはず、直ったら出す

#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};
long double eps = 1e-9;
long double pi = acos(-1);


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    int n;
    cin>>n;
    static int e[2001][2001];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>e[i][j];
        }
    }
    int INF = 1e9+7;
    static int dp[2001][2001];
    static array<int,2> pos[2001][2001];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            dp[i][j] = INF;
            pos[i][j] = {-1,-1};
        }
    }
    dp[0][0] = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i+1 < n){
                if(chmin(dp[i+1][j], dp[i][j] + e[i][i+1])){
                    pos[i+1][j] = {i,j};
                }
                if(i+1 > j and chmin(dp[i][i+1], dp[i][j] + e[i+1][j])){
                    pos[i][i+1] = {i,j};
                }
            }
            if(j+1 < n){
                if(chmin(dp[i][j+1], dp[i][j] + e[j+1][j])){
                    pos[i][j+1] = {i,j};
                }
                if(j+1 > i and chmin(dp[j+1][j], dp[i][j] + e[i][j+1])){
                    pos[j+1][j] = {i,j};
                }
            }
        }
    }
    cout << dp[n-1][n-1] << endl;

    vector<int> l,r;
    // l.push_back(0);
    int x=n-1, y=n-1;
    l.push_back(x);
    while(x || y){
        auto [nx,ny] = pos[x][y];
        if(nx != x){
            l.push_back(nx);
        }
        if(ny != y){
            r.push_back(ny);
        }
        swap(nx,x);
        swap(ny,y);
    }
    reverse(all(l));
    cout << l.size() + r.size() << endl;
    for(auto i:l)cout << i+1 << " ";
    for(int i=0;i<(int)r.size();i++){
        if(i)cout << " ";
        cout << r[i]+1;
    }
    cout << endl;
}
accuracy: 1
time_ms: 210
memory_kb: 66024
final_rank: 2

code_snippet: // これでACになるはず、直ったら出す

#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};
long double eps = 1e-9;
long double pi = acos(-1);


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    int n;
    cin>>n;
    static int e[1001][1001];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>e[i][j];
        }
    }
    int INF = 1e9+7;
    static int dp[1001][1001];
    static array<int,2> pos[1001][1001];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            dp[i][j] = INF;
            pos[i][j] = {-1,-1};
        }
    }
    dp[0][0] = 0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i+1 < n){
                if(chmin(dp[i+1][j], dp[i][j] + e[i][i+1])){
                    pos[i+1][j] = {i,j};
                }
                if(i+1 > j and chmin(dp[i][i+1], dp[i][j] + e[i+1][j])){
                    pos[i][i+1] = {i,j};
                }
            }
            if(j+1 < n){
                if(chmin(dp[i][j+1], dp[i][j] + e[j+1][j])){
                    pos[i][j+1] = {i,j};
                }
                if(j+1 > i and chmin(dp[j+1][j], dp[i][j] + e[i][j+1])){
                    pos[j+1][j] = {i,j};
                }
            }
        }
    }
    cout << dp[n-1][n-1] << endl;

    vector<int> l,r;
    // l.push_back(0);
    int x=n-1, y=n-1;
    l.push_back(x);
    while(x || y){
        auto [nx,ny] = pos[x][y];
        if(nx != x){
            l.push_back(nx);
        }
        if(ny != y){
            r.push_back(ny);
        }
        swap(nx,x);
        swap(ny,y);
    }
    reverse(all(l));
    cout << l.size() + r.size() << endl;
    for(auto i:l)cout << i+1 << " ";
    for(int i=0;i<(int)r.size();i++){
        if(i)cout << " ";
        cout << r[i]+1;
    }
    cout << endl;
}
accuracy: 0.5172413793103449
time_ms: 10
memory_kb: 17692
final_rank: 20

code_snippet: // May this submission get accepted
#include <bits/stdc++.h>

// エイリアス
using  ll = long signed long;
using ull = long unsigned long;
using  ld = long double;
using namespace std;

// AtCoder/Codeforces 用 デバッグ検知
#ifdef ONLINE_JUDGE
constexpr bool DEBUG_MODE = false;
#else
constexpr bool DEBUG_MODE = true;
#endif

// エイリアス (補完・コンパイルが重くなる)
// #include <boost/multiprecision/cpp_int.hpp>
// using mll = boost::multiprecision::cpp_int;

// 汎用マクロ
#define ALLOF(x) (begin(x)), (end(x))
#define RPE REP
#define REP(i,n) for (long long i=0, i##_len=(n); i<i##_len; i++)
#define RANGE(i,is,ie) for (long long i=(is), i##_end=(ie); i<=i##_end; i++)
#define DSRNG(i,is,ie) for (long long i=(is), i##_end=(ie); i>=i##_end; i--)
#define STEP(i, is, ie, step) for (long long i=(is), i##_end=(ie), i##_step = (step); i<=i##_end; i+=i##_step)
#define UNIQUE(v) do { sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end()); } while (false)
#define FOREACH(i,q) for (auto &i : q)
template<typename T, typename U> bool chmax(T &a, const U b) { if (a < b) {a = b; return true;} return false; }
template<typename T, typename U> bool chmin(T &a, const U b) { if (a > b) {a = b; return true;} return false; }
constexpr int INF = numeric_limits<int>::max();
constexpr long long LINF = numeric_limits<long long>::max();
constexpr long double EPS = 1e-10L;
#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")
#define IIF(q,t,f) ((q) ? (t) : (f))
#define DUMP(q) DUMP_FUNC(q, #q, __FILE__, __LINE__)
template <typename T> void DUMP_PROC(T x) { if (is_integral<T>() || is_floating_point<T>()) cerr << "\e[32m" << x << "\e[m"; else cerr << x; }
template<> void DUMP_PROC<char>(char x) { cerr << "\e[36m\'" << x << "\'\e[m"; }
template<> void DUMP_PROC<string>(string x) { cerr << "\e[33m\"" << x << "\"\e[m"; }
template <typename T, typename U> void DUMP_PROC(pair<T, U> x) { cerr << "{"; DUMP_PROC(x.first); cerr << ", "; DUMP_PROC(x.second); cerr << "}"; }
template <typename ...T, typename U, U... Seq> void DUMP_PROC(tuple<T...> &x, integer_sequence<U, Seq...>) { (void)(int[]){(cerr << ((const char*[]){"", ", "})[!!Seq] << (DUMP_PROC(get<Seq>(x)), ""), 0)...}; }
template <typename ...T> void DUMP_PROC(tuple<T...> x) {cerr << "{"; DUMP_PROC(x, index_sequence_for<T...>()); cerr << "}";}
template <typename T> void DUMP_PROC(vector<T> x) { cerr << "["; for (auto &xi : x) { DUMP_PROC(xi); cerr << (&xi != &*x.rbegin()?", ":""); } cerr << "]"; }
template <typename T> void DUMP_FUNC(T x, const char* name, const char* fn, int ln) { cerr << "\e[32m[DEBUG]\e[m " << name << ": "; DUMP_PROC(x); cerr << " @ " << fn << "(" << ln << ")" << endl; }

// gcc拡張マクロ
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

// 標準入出力
struct qin { // query input
    size_t sz;
    qin(size_t _sz = 1) : sz(_sz) {}
    template <typename T> operator T () const { T a; cin >> a; return a; }
    template <typename T> operator vector<T> () const { vector<T> a(sz); for (size_t i = 0; i < sz; i++) cin >> a[i]; return a; }
    template <typename T, typename U> operator pair<T, U> () const { T f; U s; cin >> f >> s; return pair<T, U>(f, s); }
};
qin in1; // input one
template <typename T> void say(const T x, const char* end = "\n") { cout << x << end; }
void say(const ld x, const char* end = "\n") { cout << setprecision(30) << x << end; }
template <typename T> void say(const vector<T> x, const char* sep = " ", const char* end = "\n") { REP(i, x.size()) { cout << x[i] << (i+1 == i_len ? end : sep); } }
template <typename T> void say(const vector<vector<T>> x, const char* sep = " ", const char* end = "\n") { REP(i, x.size()) { say(x[i], sep, end); } }

// モジュール
// [[LIBRARY]]
// [[/LIBRARY]]

using T = uint32_t;
using U = uint16_t;

void dfs(
    T i, T j,
    const vector<vector<pair<U, U>>> &from,
    vector<pair<U, U>> &q,
    vector<vector<bool>> &vst,
    bool &okay
) {
    q.emplace_back(i, j);
    vst[i][j] = true;
    if (q.back() == pair<U, U>(0, 0)) {
        okay = true;
        return;
    }
    {
        auto [u, v] = from[i][j];
        if (!vst[u][v]) {
            dfs(u, v, from, q, vst, okay);
            if (okay) return;
        }
    }
    q.pop_back();
}

// 処理内容
int main() {
    
    ios::sync_with_stdio(false); // stdioを使うときはコメントアウトすること
    cin.tie(nullptr);            // インタラクティブ問題ではコメントアウトすること
    
    ll n = in1;
    
    vector e(n, vector(n, (T)0));
    REP(i, n) REP(j, n) cin >> e[i][j];

    constexpr T TINF = numeric_limits<T>::max() / 2;
    vector dp(n, vector(n, TINF));
    dp[0][0] = 0;

    vector from(n, vector(n, pair<U, U>(-1, -1)));

    // chmin(dp[u][v], term) from {i, j};
    auto transfer = [&](T term, U i, U j, U u, U v) {
        if (chmin(dp[u][v], term)) {
            from[u][v] = {i, j};
        }
    };

    REP(i, n) REP(j, n) {
        if (i+1 < n) {
            transfer(dp[i][j] + e[i][i+1], i, j, i+1, j);
        }
        if (j+1 < n) {
            transfer(dp[i][j] + e[j+1][j], i, j, i, j+1);
        }
        if (i < j) {
            transfer(dp[i][j] + e[i][j], i, j, j, j);
            if (j+1 < n) {
                transfer(dp[i][j] + e[i][j+1], i, j, j+1, j);
            }
        }
        if (i > j) {
            transfer(dp[i][j] + e[i][j], i, j, i, i);
            if (i+1 < n) {
                transfer(dp[i][j] + e[i+1][j], i, j, i, i+1);
            }
        }
    }

    T cmin = dp[n-1][n-1];
    e.clear();
    e.shrink_to_fit();
    dp.clear();
    dp.shrink_to_fit();

    vector<pair<U, U>> q;
    vector vst(n, vector(n, false));
    bool okay = false;
    dfs(n-1, n-1, from, q, vst, okay);

    reverse(ALLOF(q));

    vector<U> s = {1};
    vector<U> t = {1};
    REP(k, q.size()-1) {
        auto [i, j] = q[k];
        auto [u, v] = q[k+1];
        if (i != u) {
            s.emplace_back(u+1);
        }
        if (j != v) {
            t.emplace_back(v+1);
        }
    }
    q.clear();
    q.shrink_to_fit();
    t.pop_back();
    reverse(ALLOF(t));
    copy(ALLOF(t), back_inserter(s));

    say(cmin);
    say(s.size());
    say(s, " ", "\n");
    
}
accuracy: 1
time_ms: 230
memory_kb: 50564
final_rank: 1

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

#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimize("O3,unroll-loops")
using ll = long long;
using ull = unsigned long long;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) for (int(i) = 0; (i) < int(n); (i)++)
#define repi(i, a, b) \
    for (int(i) = (a); (i) < int(b); (i)++)
#define rep(...) \
    _overload3(__VA_ARGS__, repi, _rep)(__VA_ARGS__)
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;
}
constexpr int dy[4] = {-1, 0, 1, 0};
constexpr int dx[4] = {0, -1, 0, 1};
constexpr ll MOD = 1'000'000'007LL; /*998'244'353LL;*/

int N;
int E[2000][2000];
int dp[2000][2000][2];
tuple<int, int, int, int, int> rec[2000][2000][2];
const int INF = 1e9;

signed main() {
    cin >> N;
    rep(i, N) rep(j, N) cin >> E[i][j];

    rep(i, N) rep(j, N) rep(k, 2) dp[i][j][k] = INF;
    dp[0][0][0] = 0;
    rep(i, N - 1) {
        rep(j, i + 1) {
            if (chmin(dp[i + 1][j][0],
                      dp[i][j][0] + E[i][i + 1]))
                rec[i + 1][j][0] = {i, j, 0, i + 1, -1};
            if (chmin(dp[i + 1][i][1],
                      dp[i][j][0] + E[i + 1][j]))
                rec[i + 1][i][1] = {i, j, 0, -1, i + 1};
            if (chmin(dp[i + 1][i + 1][0], dp[i][j][0] +
                                               E[i][i + 1] +
                                               E[i + 1][j]))
                rec[i + 1][i + 1][0] = {i, j, 0, i + 1,
                                        i + 1};

            if (chmin(dp[i + 1][j][1],
                      dp[i][j][1] + E[i + 1][i]))
                rec[i + 1][j][1] = {i, j, 1, -1, i + 1};
            if (chmin(dp[i + 1][i][0],
                      dp[i][j][1] + E[j][i + 1]))
                rec[i + 1][i][0] = {i, j, 1, i + 1, -1};
            if (chmin(dp[i + 1][i + 1][0], dp[i][j][1] +
                                               E[i + 1][i] +
                                               E[j][i + 1]))
                rec[i + 1][i + 1][0] = {i, j, 1, i + 1,
                                        i + 1};
        }
    }
    cout << dp[N - 1][N - 1][0] << endl;

    int i = N - 1, j = N - 1, k = 0;
    auto tmp = rec[i][j][k];
    i = get<0>(tmp);
    j = get<1>(tmp);
    k = get<2>(tmp);

    deque<int> ans;
    ans.pb(N);
    while (i > 0 || j > 0) {
        auto tmp = rec[i][j][k];
        if (get<3>(tmp) != -1)
            ans.push_front(get<3>(tmp) + 1);
        if (get<4>(tmp) != -1)
            ans.push_back(get<4>(tmp) + 1);
        i = get<0>(tmp);
        j = get<1>(tmp);
        k = get<2>(tmp);
    }
    ans.push_front(1);
    ans.push_back(1);

    cout << ans.size() << endl;
    rep(i, ans.size()) {
        cout << ans[i];
        if (i < ans.size() - 1)
            cout << " ";
        else
            cout << endl;
    }
}
accuracy: 1
time_ms: 690
memory_kb: 204536
final_rank: 17