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>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>

#define f first
#define s second
#define mp make_pair

#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()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

int a[100];
int b[100];
int memo[100][100];

int solve(int pos, int num){
  int ret = INT_MAX;
  if(num == 1) return 0;
  if(memo[pos][num]) return memo[pos][num];
  REP(i,num-1){
    int t = pos + i;
    int cost = solve(pos,i+1) + solve(pos+i+1,num-i-1);
    cost += a[pos] * b[t] * a[t+1] * b[pos+num-1];
    ret = min(ret, cost);
  }
  return memo[pos][num] = ret;
}

int main(){
  int n;
  scanf("%d",&n);
  REP(i,n) scanf("%d%d",&a[i],&b[i]);
  printf("%d",solve(0,n));
  return 0;
}

C++: #include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
// #define int ll
using PII = pair<int, int>;
 
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
 
template<typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
 
template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts) { 
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type
fill_v(T &t, const V &v) { t=v; }
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t, const V &v ) { for(auto &e:t) fill_v(e,v); }
 
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')'; return out;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
  for(T& x: vec) {is >> x;} return is;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'['; for(T i: a) {out<<i<<',';} out<<']'; return out;
}
 
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL
const int INF = 1<<30;
const ll LLINF = 1LL<<60;
ll MOD = 1000000007;

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

  ll n;
  cin >> n;
  vector<ll> a(n), b(n);
  REP(i, n) cin >> a[i] >> b[i];

  auto dp = make_v<ll>(n,n);
  fill_v(dp, LLINF);
  REP(i, n) dp[i][i] = 0;
  FOR(w, 2, n+1) {
    REP(l, n) {
      ll r = l + w - 1;
      if(r >= n) break;
      FOR(i, l, r) {
        ll cost = a[l]*b[i]*a[i+1]*b[r];
        chmin(dp[l][r], dp[l][i] + dp[i+1][r] + cost);
      }
    }
  }
  cout << dp[0][n-1] << endl;

  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
ll n;
ll INF = (1LL<<34);
P t[100];
ll dp[100][100];

P on(P x,P y){
  return P(x.first,y.second);
}

ll cost(P x,P y){
  return x.first*x.second*y.first*y.second;
}

int compute(int a,int b){
  ll A,B;
  if(dp[a][b]!=-1)return dp[a][b];
  if(a==b)return 0;
  if(a+1==b)return cost(t[a],t[b]);
  if(a+2==b){
    A  = cost(t[a],t[a+1]);
    A += cost( on(t[a],t[a+1])  ,t[b]);
    B  = cost(t[b-1],t[b]);
    B += cost(t[a], on(t[b-1],t[b]) );
    return (A<B?A:B);
  }

  ll res=INF;
  ll sum;
  P X,Y;
  for(int i=a;i<b;i++){
    X=on(t[a],t[i]);
    Y=on(t[i+1],t[b]);
    
    sum=cost(X,Y);
    sum = compute(a,i)+ sum +compute(i+1,b) ;
    res=(sum<res?sum:res);

  }
  dp[a][b]=res;
  return res;
}

int main(){

  while(cin>>n&&n){
    for(int i=0;i<n;i++)cin>>t[i].first>>t[i].second;
    for(int i=0;i<100;i++)for(int j=0;j<100;j++)dp[i][j]=-1;
    cout<<compute(0,n-1)<<endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000000000ll
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<eps)
ll dp[105][105];
ll top[105];
ll bottom[105];
ll rec(int a,int b)//[a,b]
{
	if(dp[a][b]!=-1)return dp[a][b];
	if(a==b)return 0;
	ll res=INF;
	for(int i=a;i<b;i++)
	{
		res=min(res,rec(a,i)+rec(i+1,b)+top[a]*bottom[i]*top[i+1]*bottom[b]);
	}
	return dp[a][b]=res;
}
int main()
{
	int n;
	cin >> n;
	memset(dp,-1,sizeof(dp));
	for(int i=0;i<n;i++)cin >> top[i] >> bottom[i];
	cout << rec(0,n-1) << 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-1)

const double EPS = 1e-10;

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

	vvi num(n+1, vi(2));
	vvi cost(n+1, vi(n+1, 1000000));

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

	REP(i, n+1) {
		cost[1][i] = 0;
	}
	
	FOR(i, 2, n) {
		FOR(j, 1, n-i+1) {
			FOR(k, j, i+j-2) {
				int mul = num[j][0] * num[k][1] * num[k+1][0] * num[i+j-1][1];
				cost[i][j] = min(cost[i][j], mul+cost[k-j+1][j]+cost[i+j-k-1][k+1]);
			}
		}
	}

	cout << cost[n][1] << endl;
	return 0;
}