fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  4. #define ll long long
  5. #define ld long double
  6. #define pb push_back
  7. #define fe first
  8. #define se second
  9. #define st(x) sort(x.begin(),x.end())
  10. #define rst(x) sort(x.rbegin(), x.rend())
  11. #define all(x) x.begin(),x.end()
  12. long double pi = 3.14159265358979323;
  13. #define int long long
  14. const double EPS = 1e-12;
  15. const int N = 1e5 + 5;
  16. const int mod = 998244353;
  17.  
  18. vector < int > v[N];
  19. int deg[N], let[N], inp[N];
  20. bool vis[N];
  21. int a[N];
  22. void fr(int n) {
  23. for(int i = 1; i <= n; ++i) {
  24. v[i].clear();
  25. a[i] = 0;
  26. vis[i] = 0;
  27. deg[i] = 0;
  28. let[i] = 0;
  29. }
  30.  
  31. }
  32.  
  33. void solve() {
  34. int n, m, l, r, ans(0);
  35. cin >> n >> m;
  36.  
  37. for(int i = 1; i <= m; ++i) {
  38. cin >> l >>r;
  39. v[l].pb(r);
  40. v[r].pb(l);
  41. deg[l] = v[l].size();
  42. deg[r] = v[r].size();
  43. }
  44. for(int i = 1; i <= n; ++i) {
  45. cin >> a[i];
  46. }
  47. sort(a + 1, a + 1 + n, greater < int > ());
  48. set < pair < int, int > > s;
  49. for(int i = 1; i <= n; ++i){
  50. s.insert({deg[i], i});
  51. }
  52. /*for(auto i : s)
  53. cout << i.fe << ' ' << i.se << '\n';cout << '\n';*/
  54. int i = 1;
  55. while(!s.empty()) {
  56. auto it = --s.end();
  57. if(vis[it->se]) {
  58. s.erase(it);
  59. continue;
  60. }
  61. for(auto i : v[it->se]) {
  62. if(deg[i]) {
  63. if(vis[i])
  64. continue;
  65. s.erase({deg[i], i});
  66. deg[i]--;
  67. if(deg[i])
  68. s.insert({deg[i], i});
  69. }
  70. }
  71. vis[it->se] = 1;
  72. let[it->se] = a[i++];
  73. s.erase(it);
  74. }
  75.  
  76. for(int i = 1; i <= n; ++i) {
  77. // cout << let[i] << '\n';
  78. for(auto it : v[i]) {
  79. if(it > i)
  80. ans += max(let[i], let[it]);
  81. }
  82. }
  83. cout << ans << '\n';
  84. fr(n);
  85. }
  86.  
  87. signed main(){
  88. fast;
  89. ll t ;
  90. cin >> t;
  91. while(t--)
  92. {
  93. solve();
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108. return 0;
  109.  
  110. }
Success #stdin #stdout 0s 20808KB
stdin
1
3 3
1 2
1 3
2 3
5 10 8
stdout
28