OIG XIV - lan

// https://szkopul.edu.pl/problemset/problem/NDFoTgUlg04gB-sRhLTOUHn9/site/?key=statement

#include <bits/stdc++.h>

using namespace std;

#define int long long

constexpr int sizik = 1000 * 1001;

#define ar std::array
#define pr std::pair
#define vec std::vector

typedef vec<vec<int>> _kra;

std::string toBinaryString(int num) {
    std::string binary = std::bitset<3>(num).to_string();
    return binary.empty() ? "0" : binary;
}

struct Point {
    int x, y;
    Point(int x1, int y1) : x(x1), y(y1) { ; }
    Point() { *this = Point(0, 0); }
};
struct Line {
    int a, b, c;
    Line(int a1, int b1, int c1) : a(a1), b(b1), c(c1) { ; }
    Line() { *this = Line(0, 0, 0); }
};

Point p[sizik];
int val1[sizik], val2[sizik];
Line l[sizik];

int rep[sizik];
int Find(int a) {
    if (rep[a] != a) rep[a] = Find(rep[a]);
    return rep[a];
}
void Union(int a, int b) {
    rep[Find(a)] = rep[Find(b)];
}
void DSU_init(int n) {
    for (int i = 0; i <= n; i++) {
        rep[i] = i;
    }
}

void solve() {
    int n;
    std::cin >> n;

    for (int i = 1; i <= n; i++) {
        int x, y;
        std::cin >> x >> y;
        p[i] = {x, y};
    }

    int m;
    std::cin >> m;

    for (int i = 1; i <= m; i++) {
        int a, b, c;
        std::cin >> a >> b >> c;
        l[i] = {a, b, c};
    }

    std::set<int> s;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (l[i].a * p[j].x + l[i].b * p[j].y + l[i].c > 0) {
                val1[j] = (val1[j] | (1ll << (int)(i - 1)));
            }
        }
    }

    for (int j = 1; j <= n; j++) {
        s.insert(val1[j]);
    }

    DSU_init((int)s.size());
    std::vector<Line> v;
    std::map<int, int> mp;
    int o = 1;
    for (const auto& a : s) {
        mp[a] = o++;
    }
    for (const auto& a : s) {
        for (const auto& b : s) {
            if (a == b) continue;
            v.push_back({mp[a], mp[b], __builtin_popcountll(a ^ b)});
        }
    }
    std::sort(v.begin(), v.end(), [](const Line& a, const Line& b) { return a.c < b.c; });

    int ans = 0;
    for (const auto& [a, b, c] : v) {
        if (Find(a) == Find(b)) continue;
        ans += c;
        Union(a, b);
    }

    std::cout << ans << '\n';
}

int32_t main() {
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);

    int t = 1;
    // std::cin >> t;

    for (; t > 0; t--) {
        solve();
    }

    return 0;
}