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<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) REP((i),0,(n))
#define RREP(i,a,b) for(int i=(int)(a);i>=(int)(b);i--)
#define rrep(i,n) RREP(i,n,0)
#define each(it,X) for(__typeof((X).begin())  it=(X).begin();it!=(X).end();it++)
#define all(v) ((v).begin(),(v).end())
#define fi first
#define se second
#define pb push_back
typedef pair<int,int>pr;
typedef vector<int>vi;
typedef vector<vi>vvi;
typedef set<int>si;
const int INF=1000000000000000ll;
const int mod=1000000007ll;

int n,a[100],b[100];
int dp[100][100];
signed main(){
    cin.tie(0);ios_base::sync_with_stdio(0);

    cin>>n;rep(i,n)cin>>a[i]>>b[i];
    fill_n(*dp,10000,INF);
    rep(i,n)dp[i][i]=0;

    REP(i,1,n){
        for(int j=0;j+i<n;j++){
            ///[j,j+i]
            int mi=INF;
            for(int k=j;k<j+i;k++){
                int cost=dp[j][k]+dp[k+1][i+j];
                cost+=a[j]*b[k]*a[k+1]*b[i+j];
                mi=min(mi,cost);
            }
            dp[j][j+i]=mi;
        }
    }
    cout<<dp[0][n-1]<<endl;
    return 0;
}

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

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplece_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define Edge pair< i_i, int>
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
#define int2(x, y) int x, y; scanf("%lld %lld", &x, &y);
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
 
//int dxy[5] = {0, 1, 0, -1, 0};
// assign

vvi dp;
vp data(510);
int n;

int solve(int l, int r)
{
    int ret = INF;
    if (dp[r][l] != INF) {
        return dp[l][r];
    } else if (l == r) {
        dp[l][r] = 0;
        return 0;
    } else {
        for (int i = l; i < r; i++) {
            ret = min(ret, solve(l, i) + solve(i+1, r) + 
                    data[l].fir * data[i].sec *
                    data[i+1].fir * data[r].sec);
        }
    }
    return ret;
}

signed main()
{
    cin >> n;
    dp = vvi(n, vi(n, INF));
    rep(i, n) {
        cin >> data[i].fir >> data[i].sec;
    }
    cout << solve(0, n-1) << endl;


    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()

typedef pair<int,int> pii;
int const INF = 1<<30;

int main() {
  int n;
  cin >> n;
  vector<int> v1(n),v2(n);
  REP(i,n) {
    cin >> v1[i] >> v2[i];
  }
  int ans = 0;
  while(v1.size()>1) {
    pii t1(INF,0),t2,b1(INF,0),b2;
    REP(i,n) {
      if (t1>pii(v1[i],i)) {
        t2 = t1;
        t1 = pii(v1[i],i);
      } else {
        t2 = min(pii(v1[i],i),t2);
      }
      if (b1>pii(v2[i],i)) {
        b2 = b1;
        b1 = pii(v2[i],i);
      } else {
        b2 = min(pii(v2[i],i),b2);
      }
    }
    int ue,shita;
    if (t1.second == b1.second) {
      if (t2.first < b2.first) {
        ue = t2.second;
        shita = b1.second;
      } else {
        ue = t1.second;
        shita = b2.second;
      }
    } else {
      ue = t1.second;
      shita = b1.second;
    }
    // cout << ue << " " << shita<< endl;
    ans += v1[ue]*v2[ue]*v1[shita]*v2[shita];
    v1.push_back(v1[ue]);
    v2.push_back(v2[shita]);
    v1.erase(v1.begin()+ue);
    v1.erase(v1.begin()+shita);
    v2.erase(v2.begin()+ue);
    v2.erase(v2.begin()+shita);
  }
  cout << ans << endl;
}