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++: # define _CRT_SECURE_NO_WARNINGS 1
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <forward_list>
# include <map>
# include <stack>
using namespace std;

# define INF ((int)(1<<25))
# define REP(i,n) for(int i=0;i<(int)n;i++)
# define FOR(i,n) REP(i,n)
# define TORAD 2.0*M_PI/360.0
# define INT(x) int x;cin>>x;
# define ALL(x) (x).begin(),(x).end()
# define RALL(x) (x).rbegin(),(x).rend()
# define DEBUG(x) cout<<#x<<" :"<<x<<endl;
# define EPS 1e-12



template<class T> void debug(T a) { for (auto i : a)cout << i << endl; }

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };

int n;
vector<pii> a;

int hoge(int t,int d,vi &v)
{
	if (accumulate(ALL(v),0) == n)return 0;
	int ans = INT_MAX;
	for (int i = 0; i < n; i++)
	{
		if (v[i])continue;
		v[i] = 1;
		ans = min(ans, hoge(t, a[i].second,v) + t*d*a[i].first*a[i].second);
		v[i] = 0;
	}
	return ans;
}

int main()
{
	
	cin >> n;
	FOR(i, n)
	{
		pii p; cin >> p.first >> p.second;
		a.push_back(p);
	}
	vi v(n, 0); int ans = INT_MAX;
	FOR(i, n)
	{
		v[i] = 1;
		ans = min(ans, hoge(a[i].first, a[i].second, v));
		v[i] = 0;
	}
	cout << ans << endl;

}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

int a[101], b[101];

//??????dp
int dp[101][101] = {};
int dfs(int l, int r) {
	if (dp[l][r] != INF)return dp[l][r];
	if (r - l == 1)
		dp[l][r] = 0;
	else if (r - l == 2)
		dp[l][r] = a[l] * b[l] * a[l + 1] * b[l + 1];
	else if (r - l >= 3)
		rep(m, l + 1, r) dp[l][r] = min(dp[l][r], dfs(l, m) + dfs(m, r) + a[l] * b[m - 1] * a[m] * b[r - 1]);
	return dp[l][r];
}

signed main() {
	memset(dp, 0x3f, sizeof(dp));
	int n; cin >> n;
	rep(i, 0, n)cin >> a[i] >> b[i];
	cout << dfs(0, n) << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

const long double PI = acos(-1);
constexpr long double EPS = 1e-15;
constexpr int inf = 2e9;
constexpr ll INF = 2e18;
constexpr ll MOD = 1e9+7;
constexpr ll MOD1 = 998244353;
typedef pair<ll,ll> P;

//#define all(v) (v).begin(), (v).end()
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
//#define mp make_pair

//jから始まる長さiのスタックの最小コスト
ll dp[110][110];

int main(){
    int n;
    cin >> n;
    vector<pair<ll,ll>> v;
    REP(i,n) {
        int t,b;
        cin >> t >> b;
        v.pb({t,b});
    }
    REP(i,110) REP(j,110) dp[i][j] = inf;
    for (int w = 1; w <= n; w++) {
        for (int from = 0; from + w - 1 < n; from++) {
            int to = from + w - 1;
            if (from == to) {
                dp[w][from] = 0;
                continue;
            }
            for (int l1 = 1; l1 < w; l1++) {
                int l2 = w - l1;
                dp[w][from] = min(dp[w][from], dp[l1][from] + dp[l2][from + l1] + v[from].fi * v[from + l1 - 1].se * v[from + l1].fi * v[to].se);
            }
        }
    }
    // for (int i = 0; i <= n; i++) {
    //     for (int j = 0; j <= n; j++) {
    //         cout << dp[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    cout << dp[n][0] << endl;
}

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

using namespace std;
typedef pair<int, int> P;
static const int INF = 1e9;
int dp[101][101];

void solve()
{
	int n;
	cin >> n;
	vector<P> card(n);
	for (int i = 0; i < n; ++i)
	{
		cin >> card[i].first >> card[i].second;
	}
	for (int i = 0; i <= n; ++i)
	{
		for (int j = 0; j <= n; ++j)
		{
			dp[i][j] = INF;
		}
		dp[i][i] = 0;
	}
	for (int i = 1; i < n; ++i)
	{
		for (int j = 0; j <= n - i; ++j)
		{
			for (int k = j; k < j + i; ++k)
			{
				int cost = card[j].first * card[k].second * card[k + 1].first * card[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;
}

int main()
{
	solve();
	return(0);
}

C++: // 2011/10/02 Tazoe

#include <iostream>
using namespace std;

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

	int C[100][2];
	for(int i=0; i<n; i++)
		cin >> C[i][0] >> C[i][1];

	int sum = 0;
	while(n>1){
		int max = C[0][1]*C[1][0];
		int I = 0;
		for(int i=1; i<n-1; i++)
			if(max<C[i][1]*C[i+1][0]){
				max = C[i][1]*C[i+1][0];
				I = i;
			}

		sum += C[I][0]*C[I][1]*C[I+1][0]*C[I+1][1];
		C[I][1] = C[I+1][1];
		for(int i=I+1; i<n-1; i++){
			C[i][0] = C[i+1][0];
			C[i][1] = C[i+1][1];
		}
		n--;
	}

	cout << sum << endl;

	return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define in(x) cin >> x
#define out(str) cout << str << endl

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

int N,mini=1000,abpos,ipos,miniab=0,ans=0,a1,a2,b1,b2;

int main(){
    cin>>N;
    int a[N],b[N];
    rep(i,N){
        cin>>a[i]>>b[i];
        if(mini>a[i]){
            if(miniab<b[i]){
                mini=a[i];
                abpos=0;
                ipos=i;
                miniab=b[i];
            }
        }
        if(mini>b[i]){
            if(miniab<a[i]){
                mini=b[i];
                abpos=1;
                ipos=i;
                miniab=a[i];
            }
        }
    }
    if(abpos==0){
        a1=mini;
        b1=b[ipos];
        rep(i,N){
            if(i==ipos)continue;
            a2=a[i];
            b2=b[i];
            ans+=a1*b1*a2*b2;
            b1=b2;
        }
    }
    else{
        a2=a[ipos];
        b2=mini;
        rep(i,N){
            if(i==ipos)continue;
            a1=a[i];
            b1=b[i];
            ans+=a1*b1*a2*b2;
            a2=a1;
        }
    }
    out(ans);
}