#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
template<typename T>
class IdGen
{
map<T, int> v2id_;
vector<T> id2v_;
public:
int v2id(const T& v) {
if( !v2id_.count(v) ) { v2id_[v] = size(); id2v_.push_back(v); }
return v2id_[v];
}
const T& id2v(int i) const { return id2v_[i]; }
int size() const { return id2v_.size(); }
};
template<typename Vert, typename Flow, int NV=2048>
class MaxFlow
{
IdGen<Vert> idgen;
vector<int> G[NV];
Flow F[NV][NV];
public:
void addEdge( Vert s_, Vert t_, Flow f )
{
const int s = idgen.v2id(s_), t = idgen.v2id(t_);
G[s].push_back(t);
G[t].push_back(s);
F[s][t] = f;
F[t][s] = 0;
}
Flow calc( Vert s_, Vert t_ )
{
const int S = idgen.v2id(s_), D = idgen.v2id(t_);
for( Flow total=0 ;; ) {
// Do BFS and compute the level for each node.
int LV[NV] = {0};
vector<int> Q(1, S);
for(int lv=1; !Q.empty(); ++lv) {
vector<int> Q2;
for(size_t i=0; i!=Q.size(); ++i) {
const vector<int>& ne = G[Q[i]];
for(size_t j=0; j!=ne.size(); ++j)
if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S )
LV[ne[j]]=lv, Q2.push_back(ne[j]);
}
Q.swap(Q2);
}
// Destination is now unreachable. Done.
if( !LV[D] )
return total;
// Iterating DFS.
bool blocked[NV] = {};
total += dinic_dfs( S, D, LV, 0x7fffffff, blocked );
}
}
private:
Flow dinic_dfs( int v, int D, int LV[], Flow flow_in, bool blocked[] )
{
Flow flow_out = 0;
for(size_t i=0; i!=G[v].size(); ++i) {
int u = G[v][i];
if( LV[v]+1==LV[u] && F[v][u] ) {
Flow f = min(flow_in-flow_out, F[v][u]);
if( u==D || !blocked[u] && (f=dinic_dfs(u,D,LV,f,blocked))>0 ) {
F[v][u] -= f;
F[u][v] += f;
flow_out += f;
if( flow_in == flow_out ) return flow_out;
}
}
}
blocked[v] = (flow_out==0);
return flow_out;
}
};
typedef complex<int> CMP;
double outer_prod(const CMP& a, const CMP& b) { return imag(conj(a)*b); }
double inner_prod(const CMP& a, const CMP& b) { return real(conj(a)*b); }
int ccw(const CMP& a, CMP b, CMP c) {
b -= a; c -= a;
if( outer_prod(b,c) > 0 ) return +1; // counter clockwise
if( outer_prod(b,c) < 0 ) return -1; // clockwise
if( inner_prod(b,c) < 0 ) return +2; // c--[a--b] on line
if( norm(b) < norm(c) ) return -2; // [a--b]--c on line
return 0; // [a--c--b] on line
}
bool cross(int x1, int y1, int x2, int y2,
int X1, int Y1, int X2, int Y2) {
CMP p1(x1,y1);
CMP p2(x2,y2);
CMP P1(X1,Y1);
CMP P2(X2,Y2);
return ccw(p1,p2,P1)*ccw(p1,p2,P2)<=0 && ccw(P1,P2,p1)*ccw(P1,P2,p2)<=0;
}
class SegmentDrawing { public:
enum TAG {SRC, LEFT_R, RIGHT_R, LEFT_B, RIGHT_B, DST};
static const int INF = 0x3fffffff;
int maxScore(vector <int> x, vector <int> y, vector <int> redScore, vector <int> blueScore)
{
const int N = x.size();
vector<vector<bool>> cross_table(N*N, vector<bool>(N*N));
for(int i=0; i<N; ++i)
for(int k=i+1; k<N; ++k)
for(int ii=0; ii<N; ++ii)
for(int kk=ii+1; kk<N; ++kk)
cross_table[i*N+k][ii*N+kk] = cross(x[i],y[i],x[k],y[k],x[ii],y[ii],x[kk],y[kk]);
auto* mf = new MaxFlow<pair<TAG,int>, LL>;
int all = 0;
for(int i=0; i<N; ++i)
for(int k=i+1; k<N; ++k) {
int p = i*N + k;
mf->addEdge(make_pair(SRC,0), make_pair(LEFT_R,p), redScore[p]);
mf->addEdge(make_pair(LEFT_R,p), make_pair(RIGHT_R,p), INF);
mf->addEdge(make_pair(RIGHT_R,p), make_pair(DST,0), 0);
mf->addEdge(make_pair(SRC,0), make_pair(LEFT_B,p), 0);
mf->addEdge(make_pair(LEFT_B,p), make_pair(RIGHT_B,p), INF);
mf->addEdge(make_pair(RIGHT_B,p), make_pair(DST,0), blueScore[p]);
all += redScore[p];
all += blueScore[p];
}
for(int i=0; i<N; ++i)
for(int k=i+1; k<N; ++k)
for(int ii=0; ii<N; ++ii)
for(int kk=ii+1; kk<N; ++kk) {
int rp = i*N + k;
int bp = ii*N + kk;
if(cross_table[rp][bp])
mf->addEdge(make_pair(RIGHT_R,rp), make_pair(LEFT_B,bp), INF);
}
LL flow = mf->calc(make_pair(SRC,0), make_pair(DST,0));
delete mf;
return all - flow;
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, SegmentDrawing().maxScore(x, y, redScore, blueScore));}
int main(){
CASE(0)
int x_[] = {0,1,0,-1};
vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
int y_[] = {1,0,-1,0};
vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
int redScore_[] = {0, 1, 2, 3,
1, 0, 6, 4,
2, 6, 0, 5,
3, 4, 5, 0};
vector <int> redScore(redScore_, redScore_+sizeof(redScore_)/sizeof(*redScore_));
int blueScore_[] = {0, 2, 3, 7,
2, 0, 4, 6,
3, 4, 0, 5,
7, 6, 5, 0};
vector <int> blueScore(blueScore_, blueScore_+sizeof(blueScore_)/sizeof(*blueScore_));
int _ = 27;
END
CASE(1)
int x_[] = {0, 1};
vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
int y_[] = {1, 0};
vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
int redScore_[] = {0, 101, 101, 0};
vector <int> redScore(redScore_, redScore_+sizeof(redScore_)/sizeof(*redScore_));
int blueScore_[] = {0, 100, 100, 0};
vector <int> blueScore(blueScore_, blueScore_+sizeof(blueScore_)/sizeof(*blueScore_));
int _ = 101;
END
CASE(2)
int x_[] = {-3, -1, -1, 1, 1, 3};
vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
int y_[] = { 0, -2, 2, -2, 2, 0};
vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
int redScore_[] = {0, 2, 1, 2, 1, 2,
2, 0, 2, 1, 2, 1,
1, 2, 0, 2, 1, 2,
2, 1, 2, 0, 2, 1,
1, 2, 1, 2, 0, 2,
2, 1, 2, 1, 2, 0};
vector <int> redScore(redScore_, redScore_+sizeof(redScore_)/sizeof(*redScore_));
int blueScore_[] = {0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 21, 0, 0,
0, 0, 21, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0};
vector <int> blueScore(blueScore_, blueScore_+sizeof(blueScore_)/sizeof(*blueScore_));
int _ = 25;
END
CASE(3)
int x_[] = {-100, 100, 0, -10, 10, 0};
vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
int y_[] = {0, 0, 100, 10, 10, 1};
vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
int redScore_[] = { 0, 96, 96, 25, 25, 25,
96, 0, 96, 25, 25, 25,
96, 96, 0, 25, 25, 25,
25, 25, 25, 0, 10, 10,
25, 25, 25, 10, 0, 10,
25, 25, 25, 10, 10, 0};
vector <int> redScore(redScore_, redScore_+sizeof(redScore_)/sizeof(*redScore_));
int blueScore_[] = { 0, 30, 30, 20, 20, 20,
30, 0, 30, 20, 20, 20,
30, 30, 0, 20, 20, 20,
20, 20, 20, 0, 86, 86,
20, 20, 20, 86, 0, 86,
20, 20, 20, 86, 86, 0};
vector <int> blueScore(blueScore_, blueScore_+sizeof(blueScore_)/sizeof(*blueScore_));
int _ = 546;
END
CASE(4)
int x_[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
int y_[] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
int redScore_[] = {0, 15, 2, 3, 4, 5, 6, 7, 8, 9,
15, 0, 15, 2, 3, 4, 5, 6, 7, 8,
2, 15, 0, 15, 2, 3, 4, 5, 6, 7,
3, 2, 15, 0, 15, 2, 3, 4, 5, 6,
4, 3, 2, 15, 0, 15, 2, 3, 4, 5,
5, 4, 3, 2, 15, 0, 15, 2, 3, 4,
6, 5, 4, 3, 2, 15, 0, 15, 2, 3,
7, 6, 5, 4, 3, 2, 15, 0, 15, 2,
8, 7, 6, 5, 4, 3, 2, 15, 0, 15,
9, 8, 7, 6, 5, 4, 3, 2, 15, 0}
;
vector <int> redScore(redScore_, redScore_+sizeof(redScore_)/sizeof(*redScore_));
int blueScore_[] = {0, 0, 2, 3, 4, 5, 6, 7, 8, 9,
0, 0, 0, 2, 3, 4, 5, 6, 7, 8,
2, 0, 0, 0, 2, 3, 4, 5, 6, 7,
3, 2, 0, 0, 0, 2, 3, 4, 5, 6,
4, 3, 2, 0, 0, 100, 2, 3, 4, 5,
5, 4, 3, 2, 100, 0, 0, 2, 3, 4,
6, 5, 4, 3, 2, 0, 0, 0, 2, 3,
7, 6, 5, 4, 3, 2, 0, 0, 0, 2,
8, 7, 6, 5, 4, 3, 2, 0, 0, 0,
9, 8, 7, 6, 5, 4, 3, 2, 0, 0};
vector <int> blueScore(blueScore_, blueScore_+sizeof(blueScore_)/sizeof(*blueScore_));
int _ = 300;
END
/*
CASE(5)
int x_[] = ;
vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
int y_[] = ;
vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
int redScore_[] = ;
vector <int> redScore(redScore_, redScore_+sizeof(redScore_)/sizeof(*redScore_));
int blueScore_[] = ;
vector <int> blueScore(blueScore_, blueScore_+sizeof(blueScore_)/sizeof(*blueScore_));
int _ = ;
END
CASE(6)
int x_[] = ;
vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
int y_[] = ;
vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
int redScore_[] = ;
vector <int> redScore(redScore_, redScore_+sizeof(redScore_)/sizeof(*redScore_));
int blueScore_[] = ;
vector <int> blueScore(blueScore_, blueScore_+sizeof(blueScore_)/sizeof(*blueScore_));
int _ = ;
END
*/
}
// END CUT HERE