codeparrot/codecomplex huggingface dataset:
// Problem: 8_C. Looking for Order
// Time complexity: np
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * @author Egor Kulikov (kulikov@devexperts.com)
 */
public class Order implements Runnable {
	private Scanner in = new Scanner(System.in);
	private PrintWriter out = new PrintWriter(System.out);
	private int xs, ys, n;
	private int[] x, y;

	public static void main(String[] args) {
		new Thread(new Order()).start();
	}

	private void read() {
		xs = in.nextInt();
		ys = in.nextInt();
		n = in.nextInt();
		x = new int[n];
		y = new int[n];
		for (int i = 0; i < n; i++) {
			x[i] = in.nextInt();
			y[i] = in.nextInt();
		}
	}

	private void solve() {
		int[] res = new int[1 << n];
		int[] last = new int[1 << n];
		Arrays.fill(res, Integer.MAX_VALUE);
		int[] ds = new int[n];
		for (int i = 0; i < n; i++) {
			ds[i] = (x[i] - xs) * (x[i] - xs) + (y[i] - ys) * (y[i] - ys);
		}
		int[][] d = new int[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++)
				d[i][j] = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
		}
		res[0] = 0;
		for (int i = 1; i < (1 << n); i++) {
			for (int j = 0; j < n; j++) {
				if (((i >> j) & 1) != 0) {
					if (res[i - (1 << j)] + 2 * ds[j] < res[i]) {
						res[i] = res[i - (1 << j)] + 2 * ds[j];
						last[i] = i - (1 << j);
					}
					for (int k = j + 1; k < n; k++) {
						if (((i >> k) & 1) != 0) {
							if (res[i - (1 << j) - (1 << k)] + ds[j] + ds[k] + d[j][k] < res[i]) {
								res[i] = res[i - (1 << j) - (1 << k)] + ds[j] + ds[k] + d[j][k];
								last[i] = i - (1 << j) - (1 << k);
							}
						}
					}
					break;
				}
			}
		}
		int cur = (1 << n) - 1;
		out.println(res[cur]);
		while (cur != 0) {
			out.print("0 ");
			int dif = cur - last[cur];
			for (int i = 0; i < n && dif != 0; i++) {
				if (((dif >> i) & 1) != 0) {
					out.print((i + 1) + " ");
					dif -= (1 << i);
				}
			}
			cur = last[cur];
		}
		out.println("0");
	}

	private void write() {
	}

	public void run() {
		read();
		solve();
		write();
		out.close();
	}
}
// Problem: 8_C. Looking for Order
// Time complexity: np
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class LookingForOrder {
	static final int INF = (int)1e9;
	static Point a[];
	static Point o;
	static int n;
	static int dp[];
	static PrintWriter out;

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		out = new PrintWriter(System.out);

		o = new Point(sc.nextInt(), sc.nextInt());
		n = sc.nextInt();
		a = new Point[n];
		for (int i = 0; i < n; i++)
			a[i] = new Point(sc.nextInt(), sc.nextInt());
		
		dp = new int[(1 << n) + 5];
		Arrays.fill(dp, -1);
		out.println(rec(0));
		out.print(0 + " ");
		path(0);
		out.println();
		
		out.flush();
		out.close();
	}
	
	static void path(int msk) {
		if (msk == (1 << n) - 1) return;
		
		int optimal = rec(msk);
		for (int i = 0; i < n; i++) {
			if ((msk & (1 << i)) == 0) {
				if (rec(msk | (1 << i)) + 2 * o.dist(a[i]) == optimal) {
					out.print((i + 1) + " " + 0 + " ");
					path(msk | (1 << i));
					return;
				}
				
				for (int j = 0; j < n; j++) 
					if (rec(msk | (1 << i) | (1 << j)) + o.dist(a[i]) + a[i].dist(a[j]) + a[j].dist(o) == optimal) {
						out.print((i + 1) + " " + (j + 1) + " " + 0 + " ");
						path(msk | (1 << i) | (1 << j));
						return;
					}
				break;
			}
		}
	}

	static int rec(int msk) {
		if (msk == (1 << n) - 1) return 0;
		if (dp[msk] != -1) return dp[msk];
		
		int ans = INF;
		for (int i = 0; i < n; i++) {
			if ((msk & (1 << i)) == 0) {
				ans = Math.min(ans, rec(msk | (1 << i)) + 2 * o.dist(a[i]));
				for (int j = 0; j < n; j++) {
					if (i == j) continue;
					if ((msk & (1 << j)) == 0) 
						ans = Math.min(ans, rec(msk | (1 << i) | (1 << j)) + o.dist(a[i]) + a[i].dist(a[j]) + a[j].dist(o));
				}
				break;
			}
		}
		
		return dp[msk] = ans;
	}

	static class Point {
		int x, y;

		public Point(int x, int y) {
			this.x = x;
			this.y = y;
		}

		public int dist(Point p) {
			return (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y);
		}
	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		public Scanner(FileReader f) {
			br = new BufferedReader(f);
		}

		public Scanner(InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public boolean Ready() throws IOException {
			return br.ready();
		}

		public void waitForInput(long time) {
			long ct = System.currentTimeMillis();
			while(System.currentTimeMillis() - ct < time) {};
		}

	}

}
// Problem: 8_C. Looking for Order
// Time complexity: np
import java.util.*;
import static java.lang.System.*;
import static java.lang.Math.*;

public class C {
    static int[] dp;
    static int[] f;
    static void solve(){
        dp = new int[1<<n];
        f  = new int[1<<n];
        Arrays.fill(dp, 1<<29);
        dp[0] = 0;
        for(int i=0;i<(1<<n);i++){
//            out.println("i="+i);
            for(int j=0;j<n;j++){
                int ni = i | (1<<j);
                if( i != ni ){
//                    out.println("i="+i+",j="+j);
                    int v = d[j]*2 + dp[i];
                    if(v < dp[ni]){
//                        out.println("up:"+ni+" to "+v + " fw="+i);
                        dp[ni] = v;
                        f[ni] = i;
                    }

                    for(int k=j+1;k<n;k++){
                        int nni = ni | (1<<k);
                        if( ni != nni){
                            int vv = d[j] + t[j][k] + d[k] + dp[i];
                            if(vv < dp[nni]){
//                                out.println("up:"+nni+" to "+vv + " fw="+i);
                                dp[nni] = vv;
                                f[nni] = i;
                            }
                        }
                    }
                    break;
                }
            }
        }
        out.println(dp[dp.length-1]);
//        out.println("dp="+Arrays.toString(dp));
//        out.println("f="+Arrays.toString(f));

        int idx = dp.length - 1;
        out.print("0 ");
        while(idx != 0){
            int road = idx ^ f[idx];
//            out.println("idx = "+idx + " f[idx] = "+f[idx]);
            for(int i=0;i<n;i++) if( ((road>>i) & 1) == 1) out.print((i+1)+" ");
            idx = f[idx];
            out.print("0 ");
        }
        out.println();
    }
    static int[] d;
    static int[][] t;
    static int n;
    public static void main(String[] args) {
        Scanner sc = new Scanner(in);

        int x = sc.nextInt();
        int y = sc.nextInt();

        n = sc.nextInt();
        int[] dx = new int[n];
        int[] dy = new int[n];

        for(int i=0;i<n;i++){
            dx[i] = sc.nextInt();
            dy[i] = sc.nextInt();
        }

        d = new int[n];
        for(int i=0;i<n;i++){
            d[i] = (x-dx[i])*(x-dx[i]) + (y-dy[i])*(y-dy[i]);
        }

        t = new int[n][n];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                t[i][j] = (dx[i]-dx[j])*(dx[i]-dx[j]) + (dy[i]-dy[j])*(dy[i]-dy[j]);
            }
        }
//        out.println(Arrays.toString(d));
//        out.println(Arrays.deepToString(t));

        solve();
    }
}
// Problem: 8_C. Looking for Order
// Time complexity: np
import java.util.Scanner;
import java.io.PrintWriter;
import java.util.*;


public class Order8C implements Runnable {
    private Scanner in = new Scanner(System.in);
    private PrintWriter out = new PrintWriter(System.out);

    int mintime;
    String path;

    int xs;
    int ys;
    int n;
    int[] obx;
    int[] oby;


    public static void main(String[] args) {
	new Thread(new Order8C()).start();
    }

    private void read() {
	xs = in.nextInt();
	ys = in.nextInt();
	n = in.nextInt();
	obx = new int[n];
	oby = new int[n];
	for(int i = 0; i<n; i++){
	    obx[i] = in.nextInt();
	    oby[i] = in.nextInt();
	}
	
    }

    private void solve() {
	/* we will do dynamic programming over the subsets
	   best[m] will be the shortest for binary digits of m
	   being in. 
	   last[m] will be the last subset before m */

	int[] ds = new int[n];
	int[][] d = new int[n][n];

	int[] best = new int[1 << n];
	int[] last = new int[1 << n];


	for(int i = 0; i<n; i++){
	    ds[i] = (obx[i]-xs)*(obx[i]-xs) + (oby[i]-ys)*(oby[i]-ys);
	    for(int j = i+1; j<n; j++){
		d[i][j] = (obx[i]-obx[j])*(obx[i]-obx[j]) + (oby[i]-oby[j])*(oby[i]-oby[j]);
		d[j][i] = (obx[i]-obx[j])*(obx[i]-obx[j]) + (oby[i]-oby[j])*(oby[i]-oby[j]);
	    }
	}


	for(int i=0; i< (1<<n); i++){ //goes through subsets before supersets
	    best[i] = 100000000;
	}
	best[0] = 0;

	for(int i=0; i< (1<<n); i++){ //goes through subsets before supersets
	    /*if (i % 1000000 == 0 ){
		System.out.println(i);
		}*/
	    for(int j = 0; j<n; j++){
		if( ((1 << j) & i) != 0){ // if 2^j and i have bitwise sum nonzero
		    if(best[i - (1<<j)] + 2*ds[j] < best[i]){
			best[i] = best[i - (1<<j)] + 2*ds[j];
			last[i] = i - (1<< j);

		    }
		   for(int k = j+1; k<n; k++){
		       if( ((1 << k) & i) != 0){ // if 2^k and i have bitwise sum nonzero
			   if( (best[i-(1<<j) -(1<<k)] +ds[j]+ds[k]+d[j][k]) < best[i]){
			       best[i] = (best[i-(1<<j) -(1<<k)] +ds[j]+ds[k]+d[j][k]);
			       last[i] = i -(1<<j) - (1<< k);

			   }

		       }
		   }
		   break; // break here because order doesn't really matter, just knowing which pairs to roup
		}
	    }
	}
	int i = (1<<n) -1;
	mintime = best[i];
	path = "";
	
	while(i >0){
	    //System.out.println("Got to i = " + i + " and path is: " + path);
	    path = path + "0 ";
	    int dif = i-last[i]; // this has one or two 1s in binary
	    for(int j=0; j<n; j++){
		if( ((1<<j) & dif) != 0){ // if dif has a 1 in the j'th digit
		    path  = path + (j+1) + " ";
		}
	    }
	    
	    i = last[i];
	}
	path = path + "0";
    }

    private void write() {
	out.println(mintime);
	out.println(path);
    }
    public void run() {
	read();
	solve();
	write();
	out.close();
    }
}
// Problem: 8_C. Looking for Order
// Time complexity: np
import java.util.Arrays;
import java.util.Scanner;

/**
 * @author vstepanov on 3/29/2017.
 */
public class Main {
    public static void main(String[] args) {
        try(Scanner in = new Scanner(System.in)) {
            int bx = in.nextInt();
            int by = in.nextInt();
            int n = in.nextInt();
            int[][] xy = new int[n][2];
            int[] res = new int[1 << n];
            int[] last = new int[1 << n];
            for (int i = 0; i < n; i++) {
                xy[i] = new int[]{in.nextInt(), in.nextInt()};
            }
            int[] ds = new int[n];
            for (int i = 0; i < ds.length; i++) {
                ds[i] = time(xy[i][0], xy[i][1], bx, by);
            }
            int[][] d = new int[n][n];
            for (int i = 0; i < d.length; i++) {
                for (int j = 0; j < d.length; j++) {
                    d[i][j] = time(xy[i][0], xy[i][1], xy[j][0], xy[j][1]);
                }
            }
            Arrays.fill(res, Integer.MAX_VALUE);
            res[0] = 0;
            for (int i = 0; i < (1 << n); i++) {
                for (int j = 0; j < n; j++) {
                    if ((i & mask(j)) != 0) {
                        if (res[i - mask(j)] + 2*ds[j] < res[i]) {
                            res[i] = res[i - mask(j)] + 2*ds[j];
                            last[i] = i - mask(j);
                        }
                        for (int k = j + 1; k < n; k++) {
                            if ((i & mask(k)) != 0) {
                                if (res[i - mask(k) - mask(j)] + ds[j] + ds[k] + d[j][k] < res[i]) {
                                    res[i] = res[i - mask(k) - mask(j)] + ds[j] + ds[k] + d[j][k];
                                    last[i] = i - mask(j) - mask(k);
                                }
                            }
                        }
                        break;
                    }
                }
            }

            int cur = (1 << n) - 1;
            System.out.println(res[cur]);
            int k = cur;
            while (k != 0) {
                System.out.print("0 ");
                int diff = k - last[k];
                for (int i = 0; i < n && diff != 0; i++) {
                    if (((diff >> i) & 1) != 0) {
                        System.out.print((i + 1) + " ");
                        diff -= (1 << i);
                    }
                }
                k = last[k];
            }
            System.out.println("0");
        }
    }

    static int mask(int i) {
        return 1 << i;
    }

    static int time(int x, int y, int x1, int y1) {
        return (x - x1)*(x - x1) + (y - y1)*(y - y1);
    }
}
// Problem: 8_C. Looking for Order
// Time complexity: np
import java.util.Arrays;
import java.util.Scanner;


public class C
{

    public static void main(String[] args)
    {
        new C();
    }
    
    final int oo = (int)1e9;
    
    int Hx,Hy;
    
    int N;
    int[][] P;
    
    int[] memo;
    int[][] soln;
    
    int[] dist1;
    int[][] dist2;
    
    C()
    {
        Scanner in = new Scanner(System.in);
        Hx=in.nextInt();
        Hy=in.nextInt();
        
        N=in.nextInt();
        P=new int[N][2];
        for (int i=0; i<N; ++i)
        {
            P[i][0]=in.nextInt();
            P[i][1]=in.nextInt();
        }
        
        memo=new int[1<<N];
        Arrays.fill(memo, -1);
        soln=new int[2][1<<N];
        
        dist1=new int[N];
        Arrays.fill(dist1, -1);
        dist2=new int[N][N];
        for (int[] d : dist2) Arrays.fill(d, -1);
        
        int res=go((1<<N)-1);
        System.out.println(res);
        
        int set=(1<<N)-1;
        while (set>0)
        {
            System.out.print("0 ");
            System.out.print((soln[0][set]+1)+" ");
            if (soln[1][set]>-1) System.out.print((soln[1][set]+1)+" ");
            
            if (soln[1][set]>-1)
            {
                set-=((1<<soln[0][set])+(1<<soln[1][set]));
            }
            else
            {
                set-=(1<<soln[0][set]);
            }
        }
        System.out.println("0");
    }
    
    int go(int set)
    {
        if (set==0)
            return 0;
        if (memo[set]>-1)
            return memo[set];
                
        int res=oo;
        int i=0;
        while (!on(set,i)) ++i;

        res=dist(i)+go(set-(1<<i));
        soln[0][set]=i;
        soln[1][set]=-1;
        
        for (int j=i+1; j<N; ++j)
        {
            if (on(set,j))
            {
                int tmp=dist(i,j)+go(set-(1<<i)-(1<<j));
                if (tmp<res)
                {
                    res=tmp;
                    soln[0][set]=i;
                    soln[1][set]=j;
                }
            }
        }
        
        return memo[set]=res;
    }
    
    // H->i->H
    int dist(int i)
    {
        if (dist1[i]>-1) return dist1[i];
        
        int dx=P[i][0]-Hx;
        int dy=P[i][1]-Hy;
        return dist1[i]=2*(dx*dx+dy*dy);
    }
    
    // H->i->j->H
    int dist(int i, int j)
    {
        if (dist2[i][j]>-1) return dist2[i][j];
        
        int res=0,dx,dy;
        
        dx=P[i][0]-Hx;
        dy=P[i][1]-Hy;
        res+=dx*dx+dy*dy;
        
        dx=P[i][0]-P[j][0];
        dy=P[i][1]-P[j][1];
        res+=dx*dx+dy*dy;
        
        dx=P[j][0]-Hx;
        dy=P[j][1]-Hy;
        res+=dx*dx+dy*dy;
        
        return dist2[i][j]=res;
    }
    
    boolean on(int set, int loc)
    {
        return (set&(1<<loc))>0;
    }
}
// Problem: 8_C. Looking for Order
// Time complexity: np
import java.util.*;
public class c8 {
    static int n;
    static int[] ds;
    static int[][] g;
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    int x = input.nextInt(), y = input.nextInt();
    int n = input.nextInt();
    int[] xs = new int[n], ys = new int[n];
    for(int i = 0; i<n; i++)
    {
        xs[i] = input.nextInt();
        ys[i] = input.nextInt();
    }
    ds = new int[n];
    g = new int[n][n];
    for(int i = 0; i<n; i++)
    {
        ds[i] = (x - xs[i])  *  (x - xs[i]) + (y - ys[i]) * (y - ys[i]);
        for(int j = 0; j<n; j++)
        {
            g[i][j] = (xs[i] - xs[j]) * (xs[i] - xs[j]) + (ys[i] - ys[j]) * (ys[i] - ys[j]);
        }
    }
    int[] dp = new int[1<<n];
    Arrays.fill(dp, 987654321);
    dp[0] = 0;
    for(int i = 0; i<(1<<n); i++)
    {
        if(dp[i] == 987654321) continue;
        for(int a = 0; a<n; a++)
        {
            if((i & (1<<a)) > 0) continue;
            dp[i | (1<<a)] = Math.min(dp[i | (1<<a)], dp[i] + 2*ds[a]);
            for(int b = a+1; b<n; b++)
            {
                if((i & (1<<b)) > 0) continue;
                dp[i | (1<<a) | (1<<b)] = Math.min(dp[i | (1<<a) | (1<<b)], dp[i] + ds[a] + ds[b] + g[a][b]);
            }
            break;
        }
    }
    Stack<Integer> stk = new Stack<Integer>();
    stk.add(0);
    int i = (1<<n) - 1;
    //System.out.println(Arrays.toString(dp));

    trace:
    while(i > 0)
    {
        //System.out.println(i);
        for(int a = 0; a<n; a++)
        {
            if((i & (1<<a)) == 0) continue;
            if( dp[i] == dp[i - (1<<a)] + 2*ds[a])
            {
                stk.add(a+1);
                stk.add(0);
                i -= (1<<a);
                continue trace;
            }
            for(int b = a+1; b<n; b++)
            {
                if((i & (1<<b)) == 0) continue;
                if(dp[i] == dp[i - (1<<a) - (1<<b)] + ds[a] + ds[b] + g[a][b])
                {
                    stk.add(a+1);
                    stk.add(b+1);
                    stk.add(0);
                    i -= (1<<a) + (1<<b);
                    continue trace;
                }
            }
            //break;
        }
    }
    System.out.println(dp[(1<<n) - 1]);
    while(!stk.isEmpty()) System.out.print(stk.pop()+" ");
}
}
// Problem: 8_C. Looking for Order
// Time complexity: np
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

	static int n, memo[], dS[], dd[][];

	static int dp(int idx, int msk)
	{
		if(msk == (1 << n) - 1)
			return 0;
		if(memo[msk] != -1)
			return memo[msk];
		while((msk & 1 << idx) != 0)
			++idx;
		int ret = dS[idx] * 2 + dp(idx + 1, msk | 1 << idx);
		for(int i = 0; i < n; ++i)
			if((msk & 1 << i) == 0)
				ret = Math.min(ret, dS[i] + dS[idx] + dd[i][idx] + dp(idx + 1, msk | 1 << i | 1 << idx));
		return memo[msk] = ret;
	}
	
	static StringBuilder sb = new StringBuilder("0 ");
	
	static void print(int idx, int msk)
	{
		if(msk == (1 << n) - 1)
			return;
		int opt = dp(idx, msk);
		while((msk & 1 << idx) != 0)
			++idx;
		if(dS[idx] * 2 + dp(idx + 1, msk | 1 << idx) == opt)
		{
			sb.append((idx + 1) + " 0 ");
			print(idx + 1, msk | 1 << idx);
			return;
		}
		for(int i = 0; i < n; ++i)
			if((msk & 1 << i) == 0)
				if(opt == dS[i] + dS[idx] + dd[i][idx] + dp(idx + 1, msk | 1 << i | 1 << idx))
				{
					sb.append((idx + 1) + " " + (i + 1) + " 0 ");
					print(idx + 1, msk | 1 << i | 1 << idx);
					return;
				}
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		Point s = new Point(sc.nextInt(), sc.nextInt());
		n = sc.nextInt();
		Point[] a = new Point[n];
		for(int i = 0; i < n; ++i)
			a[i] = new Point(sc.nextInt(), sc.nextInt());
		dS = new int[n];
		dd = new int[n][n];
		for(int i = 0; i < n; ++i)
		{
			dS[i] = dist2(s, a[i]);
			for(int j = 0; j < n; ++j)
				dd[i][j] = dist2(a[i], a[j]);
		}

		memo = new int[1 << n];
		Arrays.fill(memo, -1);
		out.println(dp(0, 0));
		print(0, 0);
		out.println(sb);
		out.flush();
		out.close();
	}

	static int dist2(Point a, Point b) { return sq(a.x - b.x) + sq(a.y - b.y); }

	static int sq(int x) { return x * x; }

	static class Scanner 
	{
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}

		public String next() throws IOException 
		{
			while (st == null || !st.hasMoreTokens()) 
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {return Integer.parseInt(next());}

		public long nextLong() throws IOException {return Long.parseLong(next());}

		public String nextLine() throws IOException {return br.readLine();}

		public double nextDouble() throws IOException { return Double.parseDouble(next()); }

		public boolean ready() throws IOException {return br.ready();} 
	}
}