#include typedef struct { int key, id; } data; typedef struct { data obj[200001]; int size; } min_heap; void push(min_heap* h, data x) { int i = ++(h->size), j = i >> 1; data tmp; h->obj[i] = x; while (j > 0) { if (h->obj[i].key < h->obj[j].key) { tmp = h->obj[j]; h->obj[j] = h->obj[i]; h->obj[i] = tmp; i = j; j >>= 1; } else break; } } data pop(min_heap* h) { int i = 1, j = 2; data output = h->obj[1], tmp; h->obj[1] = h->obj[(h->size)--]; while (j <= h->size) { if (j < h->size && h->obj[j^1].key < h->obj[j].key) j ^= 1; if (h->obj[j].key < h->obj[i].key) { tmp = h->obj[j]; h->obj[j] = h->obj[i]; h->obj[i] = tmp; i = j; j <<= 1; } else break; } return output; } int main() { int i, N, C[200001], X[200001], Y[200001]; scanf("%d", &N); for (i = 1; i <= N; i++) scanf("%d %d %d", &(C[i]), &(X[i]), &(Y[i])); long long ans = 0; min_heap h, hh; data d; h.size = 0; hh.size = 0; for (i = 1; i <= N; i++) { if (X[i] < Y[i]) { d.key = C[i]; d.id = i; push(&h, d); } } for (i = 0; i <= N; i++) { while (h.size > 0 && h.obj[1].key == i) { d = pop(&h); d.key = Y[d.id] - X[d.id]; push(&hh, d); } while (hh.size > i) { d = pop(&hh); ans += X[d.id]; } } while (hh.size > 0) { d = pop(&hh); ans += Y[d.id]; } for (i = 1; i <= N; i++) if (X[i] == Y[i]) ans += X[i]; for (i = 1; i <= N; i++) { if (X[i] > Y[i]) { d.key = N - C[i]; d.id = i; push(&h, d); } } for (i = 0; i <= N; i++) { while (h.size > 0 && h.obj[1].key == i) { d = pop(&h); d.key = X[d.id] - Y[d.id]; push(&hh, d); } while (hh.size > i) { d = pop(&hh); ans += Y[d.id]; } } while (hh.size > 0) { d = pop(&hh); ans += X[d.id]; } printf("%lld\n", ans); fflush(stdout); return 0; }