Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n, m, t, a[2005];
- pair<int, int> b[2005];
- int main() {
- cin >> t;
- while(t--) {
- cin >> n >> m;
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- b[i] = {a[i], i + 1};
- }
- m -= (n * (n - 1)) / 2;
- if(m < 0) {
- cout << -1 << "\n";
- continue;
- }
- int cost = 0;
- vector<pair<int,int>> v;
- for(int i = 1; i <= n; i++) {
- for(int j = i + 1; j <= n; j++) {
- if(i == j) continue;
- cost += a[j] + a[i];
- v.push_back({i , j});
- }
- }
- sort(b, b + n);
- while(m--) {
- v.push_back({b[0].second , b[1].second});
- cost += b[0].first + b[1].first;
- }
- cout << cost << "\n";
- for(auto p : v)
- cout << p.first << " " << p.second << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement