/ SeriousOJ /

Record Detail

Runtime Error


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Runtime Error 2ms 576.0 KiB
#3 Runtime Error 3ms 788.0 KiB

Code

#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define nl "\n"
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define Yes cout<<"YES\n"
#define No cout<<"NO\n"
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define F first
#define sc second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define sortv(v) sort(v.begin(), v.end());
#define rsort(v) sort(v.rbegin(), v.rend());
#define revv(v) reverse(v.begin(), v.end());

const int N=1e6+9;
const int mod1=1e9+7, mod2=1e9+9;
const int base1=137, base2=277;
const long long int inf=1e9;
const long double eps=1e-9;
#define saiful_islam_bk
vector<vector<ll>>a;
bool ok(vector<ll>&a, vector<ll>&b){
    ll p=0, q=0;
    for(ll i=0; i<a.size(); i++) p+=a[i];
    for(ll i=0; i<b.size(); i++) q+=b[i];
    if(p<q) return a<b;
    else return a>b;
}
ll total(ll n, ll k){
    ll d=0;
    for(ll i=0; i<k; i++){
        vector<ll>s(n);
        for(ll j=0; j<n; j++) s[j]=a[j][i];
        sort(all(s));
        ll ps=0;
        for(ll j=0; j<n; j++){
            d+=s[j]*j-ps;
            ps+=s[j];
        }
    }
    return d;
}
void solve(){
    ll n, k; cin>>n>>k; a.resize(n);
    for(ll i=0; i<n; i++){
        a[i].resize(k);
        for(ll j=0; j<k; j++){
            cin>>a[i][j];
        }
    }
    if(n==1) return void(cout<<0<<nl);
    sort(all(a), ok);
    ll mn=LLONG_MAX;
    for(ll i=0; i<n-1; i++){
        ll p=0;
        for(ll j=0; j<k; j++){
            p+=abs(a[i][j]-a[i+1][j]);
        }
        mn=min(mn, p);
    }
    if(n==2) mn=0;
    cout<<total(n, k)-mn<<nl;
    // cout<<abs(a[0][0]-a[n-1][0])+abs(a[0][1]-a[n-1][1])<<nl;
    // cout<<mn<<nl;
}
int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
 saiful_islam_bk
 ll test=1;
//   cin>>test;
 for(ll ii=1; ii<=test; ii++){
     //cout<<"Case "<<ii<<": ";
     solve();
 }
}

Information

Submit By
Type
Submission
Problem
P1098 KuZ the Maximum spanning tree (Hard Version)
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-12 12:54:40
Judged At
2025-01-12 12:54:40
Judged By
Score
1
Total Time
3ms
Peak Memory
788.0 KiB