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<algorithm>
using namespace std;
long memo[100][100];
long a[100],b[100];
long f(int l,int r)
{
	if(memo[l][r]>0)return memo[l][r];
	else if(l==r)return 0;
	long ans=1LL<<32;
	for(int i=l;i<r;i++)
	{
		ans=min(ans,f(l,i)+f(i+1,r)+a[l]*b[i]*a[i+1]*b[r]);
	}
	return memo[l][r]=ans;
}
main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)cin>>a[i]>>b[i];
	cout<<f(0,n-1)<<endl;
}

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

#define REP(i,n,m) for(int i=n;i<m;i++)
#define rep(i,n) REP(i,0,n)
#define EREP(i,n,m) for(int i=n;i<=m;i++)
#define erep(i,n) EREP(i,0,n)

typedef pair<int,int> P;

unsigned int dp[101][101];

int main(void){
	int n; cin>>n;
	vector<P> v(n);
	rep(i,n) cin>>v[i].first>>v[i].second;

	erep(i,n){
		erep(j,n) dp[i][j] = INT_MAX;
		dp[i][i] = 0;
	}

	REP(i,1,n){
		REP(j,0,n-1){
			REP(k,j,j+i){
				int cost = v[j].first * v[k].second * v[k+1].first * v[j+i].second;
				dp[j][j+i] = min(dp[j][j+i],cost+dp[j][k]+dp[k+1][j+i]);
			}
		}
	}

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

	return 0;
}

C++: using namespace std;
#define MAX 100
#define INFTY (1<<24)

struct Card{ int top, bottom; };

Card C[MAX+1];
int N;

int getCost(){
    int cost[MAX+1][MAX+1];
    for ( int i = 1; i <= N; i++ ){
	for ( int j = 1; j <= N; j++ ) cost[i][j] = INFTY;
    }
    for ( int i = 1; i <= N; i++ ) cost[i][i] = 0;

    for ( int j = 1; j <= N-1; j++ ){
	for ( int i = 1; i <= N-j; i++ ){
	    for ( int k = i+1; k <= i+j; k++ ){
		int mul = C[i].top * C[k-1].bottom * C[k].top * C[i+j].bottom;
		cost[i][i+j] = min( cost[i][i+j], cost[i][k-1] + cost[k][i+j] + mul );
	    }
	}
    }

    return cost[1][N];
}

main(){
    cin >> N;
    for ( int i = 1; i <= N; i++ ) cin >> C[i].top >> C[i].bottom;
    cout << getCost() << endl;
}

C++: #include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 0xffffffff;

signed main()
{
	int n;
	cin >> n;
	vector<int> a(n), b(n);
	vector<vector<int>> dp(n, vector<int>(n, INF));
	for (int i = 0; i < n; i++) {
		cin >> a[i] >> b[i];
	}
	for (int i = 0; i < n; i++) {
		dp[i][i] = 0;
	}
	for (int k = 1; k < n; k++) {
		for (int i = 0; i + k < n; i++) {
			for (int j = i; j < i + k; j++) {
				dp[i][i + k] = min(dp[i][i + k], dp[i][j] + dp[j + 1][i + k] + a[i] * b[j] * a[j + 1] * b[i + k]);
			}
		}
	}
	cout << dp[0][n - 1] << endl;
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;

#define FOR(i, n) for(int i=0;i<(int)n;i++)
#define ALL(c) (c).begin(),(c).end()
#define VCI vector<int>
#define VCS vector<string>
#define pb push_back
#define SZ size()

#define INF 2147483647

int dp[110][110];

int main() {
	int n;
	cin >> n;
	
	VCI a(n);
	VCI b(n);
	for (int i=0; i<n; i++) {
		cin >> a[i] >> b[i];
		for (int j=0; j<n; j++)
			i==j ? dp[i][j] = 0 : dp[i][j] = INF;
	}

	for (int i=0; i+1<n; i++)
		dp[i][i+1] = a[i]*b[i]*a[i+1]*b[i+1];

	for (int i=2; i<=n; i++) {
		for (int j=0; j+i<n; j++) {
			for (int k=0; k<i; k++) {
				dp[j][j+i] = min(dp[j][j+i], dp[j][j+k]+dp[j+k+1][j+i]+a[j]*b[j+k]*a[j+k+1]*b[j+i]);
			}
		}
	}

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

	return 0;
}

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

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<21

using namespace std;

int main()
{
	int n;
	cin >> n;
	
	vector<pair<int,int> > v;
	int dp[101][101];

	rep(i,n)
	{
		int a,b;
		cin >> a >> b;
		v.push_back(make_pair(a,b));
	}

	rep(i,n+1)
	{
		rep(j,n+1)
		{
			dp[i][j] = INF;
		}
		dp[i][i] = 0;
	}
	
	REP(i,1,n)
	{
		rep(j,n-i+1)
		{
			REP(k,j,j+i)
			{
				int cost = v[j].first * v[k].second * v[k+1].first *  v[j+i].second;
				dp[j][j+i] = min(dp[j][j+i],cost+dp[j][k]+dp[k+1][j+i]);
			}
		}
	}
	
	cout << dp[0][n-1] << endl;

	return 0;
}

C++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

int n,a[100],b[100];
int dp[101][101];

int main(){
	cin>>n; FOR(i,n) cin>>a[i]>>b[i];
	memset(dp,0x3f,sizeof(dp));
	FOR(i,n) dp[i][i+1] = 0;
	for(int i = 2; i <= n; i++){
		for(int l = 0; l + i <= n; l++){
			int r = l + i;
			for(int k = l+1; k < r; k++){
				dp[l][r] = min(dp[l][r],dp[l][k] + dp[k][r] + a[l] * b[k-1] * a[k] * b[r-1]);
			}
		}
	}

	cout << dp[0][n] << endl;

	return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int main() {
	int n;
	cin >> n;
	vvi cost(n, vi(2));

	REP(i, n) {
		cin >> cost[i][0] >> cost[i][1];
	}

	ll num = 0;
	int max00, max11;
	int max01, max10;
	int p0, p1;

	REP(i, n-1) {
		max00 = max11 = max01 = max10 = 1;
		REP(j, n) {
			if(cost[j][0] >= max00) {
				max00 = cost[j][0];
				max01 = cost[j][1];
				p0 = j;
			}
		}

		REP(j, n) {
			if(cost[j][1] >= max11 && p0 != j) {
				max10 = cost[j][0];
				max11 = cost[j][1];
				p1 = j;
			}
		}

		cost[p0][0] = cost[p1][0]; 
		cost[p1][0] = cost[p1][1] = 0;
		p0 = p1;

		num += max00*max01*max10*max11;
	}

	cout << num << endl;
}