小时候看这集泷昇当场给了久美子七个独奏名额

· · 题解

首先可以注意到本质上是一个森林,让你连边使得任意两点之间距离之和最小。

首先考虑 a_i=0 的时候。外面的每个点一定会经过这个连通块的某个点才能到达连通块内别的点。所以经过的这个点到达别的点的距离和要尽量小才优。所以要取以它为根,\sum dis_u 最小的一个点。这个是经典问题,做法很多,题解是使用换根实现。

其实算两点距离之和还有一种算法,是枚举每条边,让这条边的权值乘上两侧的点数之积的和。根据均值不等式,两侧点数之差越大乘积越小。而每个连通块至少会用一条边连出去,故最不均匀的方式一定是菊花图,且菊花图的中心是点数最多的连通块。

菊花图的形态已经确定,贪心地让 a_i 大的尽量分配到两侧点数乘积尽量小的位置即可。

#include<bits/stdc++.h>
using namespace std;
namespace fastio
{
    const int bufl=1<<20;
    const double base1[16]={1,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9,1e-10,1e-11,1e-12,1e-13,1e-14,1e-15};
    const double base2[16]={1,1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13,1e14,1e15};
    struct IN{
        FILE *IT;char ibuf[bufl],*is=ibuf,*it=ibuf;
        IN(){IT=stdin;}IN(char *a){IT=fopen(a,"r");}
        inline char getChar(){if(is==it){it=(is=ibuf)+fread(ibuf,1,bufl,IT);if(is==it)return EOF;}return *is++;}
        template<typename Temp>inline void getInt(Temp &a){a=0;int b=0,c=getChar();while(c<48||c>57)b^=(c==45),c=getChar();while(c>=48&&c<=57)a=(a<<1)+(a<<3)+c-48,c=getChar();if(b)a=-a;}
        template<typename Temp>inline void getDouble(Temp &a){a=0;int b=0,c=getChar(),d=0;__int128 e=0,f=0;while(c<48||c>57)b^=(c==45),c=getChar();while(c>=48&&c<=57)e=(e<<1)+(e<<3)+c-48,c=getChar();if(c==46){c=getChar();while(c>=48&&c<=57)d++,f=(f<<1)+(f<<3)+c-48,c=getChar();}a=e+base1[d]*f;if(b)a=-a;}
        IN& operator>>(char &a){a=getChar();return *this;}
        IN& operator>>(char *a){do{*a=getChar();}while(*a<=32);while(*a>32)*++a=getChar();*a=0;return *this;}
        IN& operator>>(string &a){char b=getChar();while(b<=32)b=getChar();while(b>32)a+=b,b=getChar();return *this;}
        IN& operator>>(int &a){getInt(a);return *this;}
        IN& operator>>(long long &a){getInt(a);return *this;}
        IN& operator>>(__int128 &a){getInt(a);return *this;}
        IN& operator>>(float &a){getDouble(a);return *this;}
        IN& operator>>(double &a){getDouble(a);return *this;}
        IN& operator>>(long double &a){getDouble(a);return *this;}
    };
    struct OUT{
        FILE *IT;char obuf[bufl],*os=obuf,*ot=obuf+bufl;int Eps;long double Acc;
        OUT(){IT=stdout,Eps=6,Acc=1e-6;}OUT(char *a){IT=fopen(a,"w"),Eps=6,Acc=1e-6;}
        inline void ChangEps(int x=6){Eps=x;}
        inline void flush(){fwrite(obuf,1,os-obuf,IT);os=obuf;}
        inline void putChar(int a){*os++=a;if(os==ot)flush();}
        template<typename Temp>inline void putInt(Temp a){if(a<0){putChar(45);a=-a;}if(a<10){putChar(a+48);return;}putInt(a/10);putChar(a%10+48);}
        template<typename Temp>inline void putLeading(Temp a,int b){if(!b)return;putLeading(a/10,b-1);putChar(a%10+48);}
        template<typename Temp>inline void putDouble(Temp a){if(a<0){putChar(45);a=-a;}__int128 b=a;putInt(b);a-=b;a*=base2[Eps];b=a+Acc;putChar(46);putLeading(b,Eps);}
        OUT& operator<<(char a){putChar(a);return *this;}
        OUT& operator<<(char *a){while(*a>32)putChar(*a++);return *this;}
        OUT& operator<<(string a){for(auto c:a)putChar(c);return *this;}
        OUT& operator<<(int a){putInt(a);return *this;}
        OUT& operator<<(long long a){putInt(a);return *this;}
        OUT& operator<<(__int128 a){putInt(a);return *this;}
        OUT& operator<<(float a){putDouble(a);return *this;}
        OUT& operator<<(double a){putDouble(a);return *this;}
        OUT& operator<<(long double a){putDouble(a);return *this;}
        ~OUT(){flush();}
    };
}
using fastio::IN;
using fastio::OUT;
IN fin;
OUT fout;
typedef __int128 ll;
const int NN=1e6+4,P=1e9+7;
int siz[NN],a[NN];
bool vis[NN];
vector<pair<int,int> >g[NN];
ll dfs(int u,int fa)
{
    vis[u]=true;
    siz[u]=1;
    ll res=0;
    for(auto t:g[u])
    {
        int v=t.first,w=t.second;
        if(v==fa)
            continue;
        res+=dfs(v,u)+1ll*siz[v]*w;
        siz[u]+=siz[v];
    }
    return res;
}
ll minn,res;
void get_min(int u,int fa,ll now,int all)
{
    minn=min(minn,now);
    res+=now;
    for(auto t:g[u])
    {
        int v=t.first,w=t.second;
        if(v==fa)
            continue;
        get_min(v,u,now+1ll*w*(all-siz[v])-1ll*w*siz[v],all);
    }
}
int main()
{
    double be=clock();
    int n,m;
    fin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int u,v,w;
        fin>>u>>v>>w;
        g[u].push_back({v,w});
        g[v].push_back({u,w});
    }
    ll ans=0;
    vector<int>p;
    for(int i=1;i<=n;i++)
        if(!vis[i])
        {
            minn=dfs(i,0);
            res=0;
            get_min(i,0,minn,siz[i]);
            ans+=res/2+1ll*minn*(n-siz[i]);
            p.push_back(siz[i]);
        }
    sort(p.begin(),p.end(),greater<int>());
    for(int i=1;i<=n-m-1;i++)
        fin>>a[i];
    sort(a+1,a+n-m);
    for(int i=1;i<=n-m-1;i++)
        ans+=1ll*(n-p[i])*p[i]*a[i];
    fout<<ans%P;
    return 0;
}