提交时间:2024-11-21 13:31:24

运行 ID: 34994

#include<bits/stdc++.h> #define int long long using namespace std; const int maxn=257; int n; struct Node{ int a,b,c; }p[maxn]; inline bool chk(){ for(int i=1;i<n;i++){ if(p[i].a!=p[i+1].a)return 0; if(p[i].b!=p[i+1].b)return 0; } return 1; } int Per[maxn]; Node cur[maxn]; inline void BF(){ for(int i=1;i<=n;i++)Per[i]=i; int ans=3e13; do{ for(int i=1;i<=n;i++)cur[i]=p[Per[i]]; int cst=0; for(int i=2;i<=n;i++){ cst+=max(cur[i-1].a-cur[i].a,0ll); cst+=max(cur[i-1].b-cur[i].b,0ll); cst+=max(cur[i-1].c-cur[i].c,0ll); cur[i].a=max(cur[i].a,cur[i-1].a); cur[i].b=max(cur[i].b,cur[i-1].b); cur[i].c=max(cur[i].c,cur[i-1].c); } ans=min(ans,cst); }while(next_permutation(Per+1,Per+n+1)); cout<<ans<<endl; } signed main(){ cin>>n; for(int i=1;i<=n;i++)cin>>p[i].a>>p[i].b>>p[i].c; if(chk()){ cout<<0<<endl; return 0; } BF(); }