欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1255B.Fridge Lockers
Fridge Lockers
Hanh lives in a shared apartment. There are people (including Hanh) living there, each has a private fridge.
fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of people can open it.
For exampe, in the picture there are people and chains. The first person knows passcodes of two chains: and . The fridge can be open by its owner (the person ), also two people and (acting together) can open it.
The weights of these fridges are . To make a steel chain connecting fridges and , you have to pay dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly steel chains so that all fridges are private. A fridge is private if and only if, among people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge is not private if there exists the person () that the person can open the fridge .
For example, in the picture all the fridges are private. On the other hand, if there are fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). Then the descriptions of the test cases follow.
The first line of each test case contains two integers , (, ) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains integers () — weights of all fridges.
Output
For each test case:
- If there is no solution, print a single integer .
- Otherwise, print a single integer — the minimum total cost. The -th of the next lines contains two integers and (, ), meaning that the -th steel chain connects fridges and . An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Samples
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |