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<iostream>
#include<vector>

using namespace std;
int main()
{
  int n;
  for(;cin>>n;)
    {
      vector< pair<int,int> > data;
      int ans=0;
      for(int i=0;i<n;i++)
	{
	  int a,b;
	  cin>>a>>b;
	  data.push_back(make_pair(a,b));
	}
      while(data.size()>=2)
	{
	  int max=0;
	  int bit=0;
	  for(int i=1;i<data.size();i++)
	    {
	      if(data[i-1].second+data[i].first>max)
		{
		  max=data[i-1].second+data[i].first;
		  bit=i;
		}
	    }
	  ans+=data[bit-1].second*data[bit].first*data[bit].second*data[bit-1].first;
	  data.insert(data.begin()+(bit-1),make_pair(data[bit-1].first,data[bit].second));
	  data.erase(data.begin()+bit);
	  data.erase(data.begin()+bit);
	  //  for(int i=0;i<data.size();i++)
	  //   cout<<"<"<<data[i].first<<","<<data[i].second<<"> ";
	  // cout<<endl;
	}
      cout<<ans<<endl;
    }

}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <deque>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i,0,(n))
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n, m)
#define all(r) r.begin(),r.end()
#define fi first
#define se second
#define println(X) cout<<X<<endl;
 
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;

int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
int n, d;
vector<string> s;

bool outOfRange(int y, int x){
	if(y < 0 || y >= n || x < 0 || x >= n) return true;
	return false;
}

bool check(int ny, int nx, int dir){
	if(outOfRange(ny, nx)) return true;
	for(int i = 0; i < 4; i++){
		if(i == (dir+2)%4) continue;
		int nny = ny + dy[i], nnx = nx + dx[i];
		if((!outOfRange(nny, nnx)) && s[nny][nnx] == '#') return true;
	}
	return false;
}

int main(){
	cin>>d;
	for(int i = 0; i < d; i++){
		if(i!=0) printf("\n");
		cin>>n;
		s = vector<string>(n,string(n, ' '));
		pii pos = pii(n-1, 0);
		int dir = 0;
		while(1){
			if(s[pos.fi][pos.se]=='#') break;
			s[pos.fi][pos.se] = '#';
			int ny = pos.fi+dy[dir], nx = pos.se+dx[dir], cnt = 4;
			while(check(ny, nx, dir) &&(--cnt)){
				dir++; dir%=4;
				ny = pos.fi+dy[dir]; nx = pos.se+dx[dir];

			}
			if(cnt == 0) break;
			pos=pii(ny ,nx);
		}
		rep(i, n) cout<<s[i]<<endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <climits>

using namespace std;

//dp[i][j] = [i,j]をすべてまとめるときの最小コスト
int dp[128][100];

int solve(const vector<pair<int,int> > &v, int left, int right) {
    const int N = (int)v.size();
    int &res = dp[left][right];
    if(res < INT_MAX) return res;
    if(left+1 < N) {
        const int cost = v[left].first*v[left].second * v[left+1].first*v[right].second;
        res = min(res, solve(v, left+1, right) + cost);
    }
    if(right-1 >= 0) {
        const int cost = v[left].first*v[right-1].second * v[right].first*v[right].second;
        res = min(res, solve(v, left, right-1) + cost);
    }
    return res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int N;
    cin >> N;
    vector<pair<int,int> > v(N);
    for(int i = 0; i < N; ++i) {
        cin >> v[i].first >> v[i].second;
    }
    for(int i = 0; i < N; ++i) {
        for(int j = 0; j < N; ++j) {
            if(i == j) dp[i][j] = 0;
            else dp[i][j] = INT_MAX;
        }
    }
    cout << solve(v, 0, N-1) << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define chmin(a,b) (a)=min((a),(b));
#define chmax(a,b) (a)=max((a),(b));
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) cerr<<(#v)<<":";for(auto(x):(v)){cerr<<" "<<(x);}cerr<<endl;
#define printVS(vs) cerr<<(#vs)<<":"<<endl;for(auto(s):(vs)){cerr<<(s)<< endl;}
#define printVV(vv) cerr<<(#vv)<<":"<<endl;for(auto(v):(vv)){for(auto(x):(v)){cerr<<" "<<(x);}cerr<<endl;}
#define printP(p) cerr<<(#p)<<(p).first<<" "<<(p).second<<endl;
#define printVP(vp) cerr<<(#vp)<<":"<<endl;for(auto(p):(vp)){cerr<<(p).first<<" "<<(p).second<<endl;}

inline void output(){ cerr << endl; }
template<typename First, typename... Rest>
inline void output(const First& first, const Rest&... rest) {
    cerr << first << " "; output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 1ll << 60;
const int mod = 1e9 + 7;
using Graph = vector<vector<int>>;

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int n;
    cin >> n;
    vector<Pii> p;
    rep(i, n) {
      int a, b;
      cin >> a >> b;
      p.emplace_back(a, b);
    }

    vvi dp(n, vi(n, inf));
    rep(i, n) dp[i][i] = 0;
    rep2(len, 2, n + 1) {
      rep(l, n) {
        int r = l + len - 1;
        if (r >= n) break;
        rep2(c, l, r) {
          chmin(dp[l][r], dp[l][c] + dp[c+1][r] +
            p[l].first * p[c].second * p[c+1].first * p[r].second);
        }
      }
    }

    cout << dp[0][n-1] << endl;
}

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 A = 0, B = 1;
		rep(j, n - i) if(v[A].a < v[j].a)
		{
			A = j;
		}
		
		rep(j, n - i) if(A != j && v[B].b < v[j].b)
		{
			B = j;
		}
		
		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;
}