Niuke's original topic, the author zb back pot.

Topic: On the Magic Matrix, choose the smallest cost and meet the requirements of the topic;

Thought: Enumerate each energy size from small to large, calculate the cost of destroying the magic array, and take the smallest, because the required magic coin is only 200 at most, so open two arrays of 200 size to enumerate the cost of violence. Look at the code comments in detail.

#include <ctime>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lson rt << 1,l,m
#define rson rt << 1 | 1 ,m + 1, r
#define accept return 0
#define  mes(a, b)  memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 1e6 + 10;
const int    maxm = 1e5 + 10;
// const int    mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-8;
using namespace std;
ll need[300],nneed[300];
ll cost = 0;
ll minn,n;

struct node{
    ll h,w,n;
    bool operator <(const node & a)const{
        return h < a.h;//Sort by energy
    }
}t[maxn];

void solve(){
    mes(need,0);
    mes(nneed,0);
    sort(t + 1 , t + 1 + n);
    ll nown = 0;
    minn = INF;
    ll ans = 0;
    ll nowin = 0;
    for(int i = 1;i <= n;i++){   //Enumeration of energy from small to large
        ans = 0;
        nneed[t[i].w] += t[i].n; //nneed array stores all the energy of the magic wand currently in possession
        nown += t[i].n;    //Number of all magic wands currently in possession
        nowin += t[i].n;    //Number of magic wands of current energy
        cost -= t[i].n * t[i].w;  //The total cost of destroying a magic wand that is more powerful than the current wand
       if(t[i].h != t[i + 1].h){
            ans += cost;
            if(nown - (nowin * 2 - 1) > 0){
                ll left = nown - (nowin * 2 - 1);  //The current number of magic wands of energy is strictly larger than all magic wands currently in possession.
                for(int j = 1;j <= 200;j++){
                    if(left > need[j]) {ans += j * need[j]; left -= need[j];} 
                    //A magic wand minus the minimum cost of a magic wand with less energy than the current wand
                    else{ans += j * left;break;}
                }
            }
            minn = min(minn,ans);
            for(int j = 1;j <= 200;j++) need[j] = nneed[j];
            nowin = 0;
        }
    }
    printf("%lld\n",minn);
}

int main(){
    while(cin >> n){
        cost = 0;
        for(int i = 1;i <= n;i++) {
            cin >> t[i].h >> t[i].w >> t[i].n;
            cost += t[i].w * t[i].n; //Maximum energy cost of pretreatment
        }
        solve();
    }
}