Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"sort"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)
const INF = 0x7fffffff
func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	top := make([]int, 0)
	bottom := make([]int, 0)
	for i := 0; i < n; i++ {
		scanner.Scan()
		buf := strings.Split(scanner.Text(), " ")
		a, _ := strconv.Atoi(buf[0])
		b, _ := strconv.Atoi(buf[1])
		top = append(top, a)
		bottom = append(bottom, b)
	}
	dp := make([][]int, n+1)
	for i := range dp {
		dp[i] = make([]int, n+1)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			dp[i][j] = INF
		}
	}
	for j := 0; j < n; j++ {
		dp[0][j] = 0
	}
	for i := 1; i < n; i++ {
		for j := 0; j < n-i; j++ {
			tmp := make([]int, 0)
			for k := 0; k < i; k++ {
				tmp = append(tmp, dp[k][j] + dp[i - k - 1][j + k + 1] +top[j] * bottom[j + k] * top[j + k + 1] * bottom[j + i])
			}
			sort.Ints(tmp)
			dp[i][j] = tmp[0]
		}
	}
	fmt.Println(dp[n-1][0])
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

C++: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

void mainmain(){
	int n;
	cin>>n;
	vector<pll> v(n);
	rep(i,3){
		cin>>v[i].F>>v[i].S;
	}
	ll ans=0;
	while(v.size()!=1){
		// rep(i,v.size()){
			// cout<<v[i].F<<" "<<v[i].S<<endl;
		// }
		// cout<<endl;
		ll x=-1,y=0;
		rep(i,v.size()-1){
			if(y<v[i].F*v[i+1].S){
				x=i;
				y=v[i].F*v[i+1].S;
			}
			if(y<v[i].S*v[i+1].F){
				x=i;
				y=v[i].S*v[i+1].F;
			}
		}
		ans+=v[x].F*v[x].S*v[x+1].F*v[x+1].S;
		if(v[x].F*v[x+1].S>v[x].S*v[x+1].F) v[x]=mkp(v[x+1].F,v[x].S);
		else v[x]=mkp(v[x].F,v[x+1].S);
		reep(i,x+1,v.size()-1){
			swap(v[i],v[i+1]);
		}
		v.pop_back();
	}
	cout<<ans<<endl;
}



}
main() try{
    mainmain();
}
catch(...){
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)

#define INF 1000000000

int top[111];
int bot[111];

int memo[111][111];
int saiki(int st,int en){
	if(st+1 == en)return 0;
	
	int cost = INF;
	
	for(int i=st+1;i<en;i++){
		int next = 0;
		next += saiki(st,i);
		next += saiki(i,en);
		next += top[st]*bot[i-1]*top[i]*bot[en-1];
		
		cost = min(next,cost);
	}
	
	return cost;
}

int main(){
	int n;
	cin>>n;
	
	rep(i,n)cin>>top[i]>>bot[i];
	
	int ans = saiki(0,n);
	printf("%d\n",ans);
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
const int INF = 1e8;
using namespace std;

int main(){
    int n;
    cin >> n;

    pair<int, int> p[105];
    rep(i,n) cin >> p[i].first >> p[i].second;

    vector<vector<vector<long long>>> dp(105,vector<vector<long long>>(205,vector<long long>(205,INF)));
    rep(l,n){
        for(int i = n; i > 1; i--){
            range(j,1,201){
                range(k,1,201){
                    dp[i][j][p[l].second] = min(dp[i][j][p[l].second], dp[i - 1][j][k] + j * k * p[l].first * p[l].second);
                    dp[i][p[l].first][k] = min(dp[i][p[l].first][k], dp[i - 1][j][k] + j * k * p[l].first * p[l].second);
                }
            }
        }
        dp[1][p[l].first][p[l].second] = 0;
    }

    long long mini = INF;
    rep(i,201) rep(j,201)  mini = min(mini, dp[n][i][j]);
    cout << mini << endl;
}

C++: #include <iostream>
#include <vector>
using namespace std;

struct card{
	int top, bottom;
};

int main(){
	int n;
	int cost = 0;
	vector<card> cards;
	cin >> n;
	for(int i = 0; i < n; i++){
		card c;
		cin >> c.top >> c.bottom; 
		cards.push_back(c);
	}
	while(cards.size() != 1){
		int t, b;
		int nmax = -1;
		for(int i = 0; i < cards.size(); i++){
			for(int j = 0; i < cards.size(); i++){
				if(i != j){
					if(cards[i].bottom * cards[j].top > nmax){
						nmax = cards[i].bottom * cards[j].top;
						t = i;
						b = j;
					}
				}
			}
		}
		cost += cards[t].top * cards[t].bottom * cards[b].top * cards[b].bottom;
		cards[t].bottom = cards[b].bottom;
		cards[b] = cards[cards.size() - 1];
		cards.pop_back();
	}
	cout << cost << endl;
	return 0;
}

C++: 
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

struct M{ int a, b; };

int main()
{
    int n;
	cin >> n;
	
	vector<M> v(n);
	rep(i, n) cin >> v[i].a >> v[i].b;
	
	int res = 0;
	rep(i, n - 1)
	{
		int A1 = 0, B1 = 1;
		rep(j, n - i) if(v[A1].a < v[j].a)
		{
			A1 = j;
		}
		
		rep(j, n - i) if(A1 != j && v[B1].b < v[j].b)
		{
			B1 = j;
		}
		
		int A2 = 1, B2 = 0;
		rep(j, n - i) if(v[B2].b < v[j].b)
		{
			B2 = j;
		}
		
		rep(j, n - i) if(B2 != j && v[A2].a < v[j].a)
		{
			A2 = j;
		}

		int A, B;
		
		if(v[A1].a + v[B1].b < v[A2].a + v[B2].b)
		{
			A = A2, B = B2;
		}
		else
		{
			A = A1, B = B1;
		}
		
		
		res += v[A].a * v[A].b * v[B].a * v[B].b;
		
		v[A].a = v[B].a;
		
		v.erase(v.begin() + B);
	}
	
	cout << res << endl;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;

int main(){
	int n;
	int a[100],b[100];
	int cb[100][100];
	//b > a
	for(int i = 0; i < 100; i++){
		for(int j = i; j < 100; j++){
			cb[i][j] = 2147483647;
		}
	}

	cin >> n;

	for (int i = 0; i < n; i++){
		cin >> a[i] >> b[i];
	}		// 入力

	for(int i = 0; i < n; i++){
		cb[i][i] = 0;
		if(i != (n - 1)){
			cb[i][i+1] = a[i] * b[i] * a[i+1] * b[i+1];
		}
	}
	for(int i = 0; i < n; i++){
		for(int j = i; j < n; j++){		//cd[i][j]
			for(int k = i; k < (j-1); k++){
				cb[i][j] = min(cb[i][j],cb[i][k]+cb[k+1][j]+a[i]*b[k]*a[k+1]*b[j]);
			}
		}
	}
	cout << cb[0][n-1] << endl;
	return 0;
}