题目传送门
#include
#define ll long long
using namespace std;
template <typename T> void read(T &x) {
x = 0; int f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
const int N = 10010, M = N * 15; const ll inf = 1ll << 60;
int cnt, tot, rt[N], b[M], l[M], lc[M], rc[M], cur[M], head[M];
struct Info {int a, b, w, l, r, p;} q[N];
struct Edge {int next, num; ll c;} e[M * 3];
void add(int x, int y, ll c) {e[++cnt] = (Edge) {head[x], y, c}, head[x] = cnt;}
void Add(int x, int y, ll c) {add(x, y, c), add(y, x, 0);}
bool bfs(int s, int t) {
for (int i = s; i <= tot; i++) l[i] = -1;
queue <int> q; q.push(s), l[s] = 0;
while (!q.empty()) {
int x = q.front(); q.pop();
for (int p = head[x]; p; p = e[p].next) {
int k = e[p].num; ll c = e[p].c;
if (c && l[k] == -1) l[k] = l[x] + 1, q.push(k);
}
}
return l[t] != -1;
}
ll dfs(int x, int t, ll lim) {
if (x == t) return lim; ll ret = 0;
for (int &p = cur[x]; p; p = e[p].next) {
int k = e[p].num; ll c = e[p].c;
if (c && l[k] == l[x] + 1) {
ll w = dfs(k, t, min(lim - ret, c));
e[p].c -= w, e[p ^ 1].c += w, ret += w;
if (ret == lim) return ret;
}
}
if (!ret) l[x] = -1; return ret;
}
ll dinic(int s, int t) {
ll ret = 0;
while (bfs(s, t)) {
for (int i = s; i <= tot; i++) cur[i] = head[i];
ret += dfs(s, t, inf);
}
return ret;
}
int ins(int k, int l, int r, int x, int id) {
int ret = ++tot; lc[ret] = lc[k], rc[ret] = rc[k];
if (l == r) {
Add(id, ret, inf);
if (k) Add(k, ret, inf);
return ret;
}
int mid = (l + r) >> 1;
if (x <= mid) lc[ret] = ins(lc[k], l, mid, x, id);
else rc[ret] = ins(rc[k], mid + 1, r, x, id);
if (lc[ret]) Add(lc[ret], ret, inf);
if (rc[ret]) Add(rc[ret], ret, inf);
return ret;
}
void query(int k, int l, int r, int L, int R, int id) {
if (!k) return;
if (L <= l && r <= R) return Add(k, id, inf), void();
int mid = (l + r) >> 1;
if (L <= mid) query(lc[k], l, mid, L, R, id);
if (R > mid) query(rc[k], mid + 1, r, L, R, id);
}
int main() {
int n, len = 0; read(n); ll ans = 0;
int s = 0, t = 2 * n + 1; cnt = 1, tot = 2 * n + 1;
for (int i = 1; i <= n; i++) {
read(q[i].a), read(q[i].b), read(q[i].w);
read(q[i].l), read(q[i].r), read(q[i].p);
ans += q[i].b + q[i].w;
Add(s, i, q[i].w), Add(i, t, q[i].b), Add(i + n, i, q[i].p);
b[++len] = q[i].a, b[++len] = q[i].l, b[++len] = q[i].r;
}
sort(b + 1, b + len + 1), len = unique(b + 1, b + len + 1) - b - 1;
for (int i = 1; i <= n; i++)
q[i].a = lower_bound(b + 1, b + len + 1, q[i].a) - b,
q[i].l = lower_bound(b + 1, b + len + 1, q[i].l) - b,
q[i].r = lower_bound(b + 1, b + len + 1, q[i].r) - b;
for (int i = 1; i <= n; i++) {
query(rt[i - 1], 1, len, q[i].l, q[i].r, i + n);
rt[i] = ins(rt[i - 1], 1, len, q[i].a, i);
}
cout << ans - dinic(s, t) << "\n";
return 0;
}