#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long long MAX_N = 1e6 + 5;   using pii = pair<long long, long long>;   struct edge {     long long to, flow, cap, cost, rev;   };   struct MinCostMaxFlow {     long long nodes;     vector<long long> prio, curflow, prevedge, prevnode, q, pot;     vector<bool> inqueue;     vector<vector<edge>> graph;     MinCostMaxFlow() {}     MinCostMaxFlow(long long n)         : nodes(n),           prio(n, 0),           curflow(n, 0),           prevedge(n, 0),           prevnode(n, 0),           q(n, 0),           pot(n, 0),           inqueue(n, 0),           graph(n) {}     void addEdge(long long source, long long to, long long capacity,                  long long cost) {       edge a = {to, 0, capacity, cost, (long long)graph[to].size()};       edge b = {source, 0, 0, -cost, (long long)graph[source].size()};       graph[source].push_back(a);       graph[to].push_back(b);     }     void bellman_ford(long long source, vector<long long> &dist) {       fill(dist.begin(), dist.end(), INT_MAX);       dist[source] = 0;       long long qt = 0;       q[qt++] = source;       for (long long qh = 0; (qh - qt) % nodes != 0; qh++) {         long long u = q[qh % nodes];         inqueue[u] = false;         for (auto &e : graph[u]) {           if (e.flow >= e.cap) continue;           long long v = e.to;           long long newDist = dist[u] + e.cost;           if (dist[v] > newDist) {             dist[v] = newDist;             if (!inqueue[v]) {               inqueue[v] = true;               q[qt++ % nodes] = v;             }           }         }       }     }     pair<long long, long long> minCostFlow(long long source, long long dest,                                            long long maxflow) {       bellman_ford(source, pot);       long long flow = 0;       long long flow_cost = 0;       while (flow < maxflow) {         priority_queue<pair<long long, long long>,                        vector<pair<long long, long long>>,                        greater<pair<long long, long long>>>             q;         q.push({0, source});         fill(prio.begin(), prio.end(), INT_MAX);         prio[source] = 0;         curflow[source] = INT_MAX;         while (!q.empty()) {           long long d = q.top().first;           long long u = q.top().second;           q.pop();           if (d != prio[u]) continue;           for (long long i = 0; i < graph[u].size(); i++) {             edge &e = graph[u][i];             long long v = e.to;             if (e.flow >= e.cap) continue;             long long newPrio = prio[u] + e.cost + pot[u] - pot[v];             if (prio[v] > newPrio) {               prio[v] = newPrio;               q.push({newPrio, v});               prevnode[v] = u;               prevedge[v] = i;               curflow[v] = min(curflow[u], e.cap - e.flow);             }           }         }         if (prio[dest] == INT_MAX) break;         for (long long i = 0; i < nodes; i++) pot[i] += prio[i];         long long df = min(curflow[dest], maxflow - flow);         flow += df;         for (long long v = dest; v != source; v = prevnode[v]) {           edge &e = graph[prevnode[v]][prevedge[v]];           e.flow += df;           graph[v][e.rev].flow -= df;           flow_cost += df * e.cost;         }       }       return {flow, flow_cost};     }   };   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n;     cin >> n;     MinCostMaxFlow network = MinCostMaxFlow(2 * n + 3);     long long s = 0;     long long t = 2 * n + 2;     for (long long i = 0; i < n; i++) {       network.addEdge(s, i + 1, 1, 0);       network.addEdge(i + 1 + n, t, 1, 0);       long long a, b, k;       cin >> a >> b >> k;       for (long long j = 0; j <= n; j++) {         long long cc = max(0LL, a - min(k, j) * b);         network.addEdge(i + 1, j + 1 + n, 1, -cc);       }     }     network.addEdge(t - 1, t, 1, 0);     cout << -network.minCostFlow(s, t, n + 1).second;     return 0;   }