QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 16 MB Puntuación total: 10

#11669. 图书馆

Estadísticas

Bajtazar 在假期里成为了文学爱好者。他从图书馆借阅了许多书。有一天,他的朋友 Wyjątek 来访,对其中一本书产生了兴趣。经过长时间的劝说,Bajtazar 忘记了之前与 Wyjątek 的不愉快,终于同意把书借给他。

假期很快结束了。新学年开始了(尽管在 Bajtocja 开始得特别晚——10 Terabajta),到了归还图书馆书籍的时间。Bajtazar 想起他把其中一本书借给了 Wyjątek。当他向他索要时,Wyjątek 承认在度假期间把书弄丢了。这让可怜的 Bajtazar 非常苦恼,因为他知道这会给他带来很多麻烦。

当他把一切告诉图书馆的女士们时,她们决定,由于他的鲁莽行为,他必须接受惩罚,并要求他在 0110(相当于我们的星期六)来帮助她们整理图书卡。由于 Bajtazar 的几位朋友已经接受过这样的惩罚,所以卡片现在分散在许多不同大小的已排序文件中,Bajtazar 的任务是将它们合并成一个文件。

由于 Bajtazar 已经提前计划好了这一天,同时又不想再次让图书管理员失望,你必须帮他编写一个程序,让他确定整理卡片需要多少时间,并确定合并文件的适当顺序,以便他能尽快完成任务。

Bajtazar 一次只能合并两个文件。为简化起见,我们假设合并两个文件的时间等于它们长度之和。

题目描述

编写一个程序,完成以下任务:

  • 读取:
    • 需要合并成一个有序文件的图书卡文件数量,
    • 文件的长度,
  • 计算:
    • 在图书馆整理卡片的最小总时间,
    • 实现该时间的合并文件顺序,
  • 输出结果。

输入格式

输入包含两行。第一行包含一个整数 $n$ ($2 \le n \le 100\,000$),表示要合并的文件数量。第二行包含 $n$ 个整数 $s_1, s_2, \dots, s_n$,由单个空格分隔,其中 $s_i$ 表示编号为 $i$ 的文件的长度 ($1 \le s_i \le 10\,000$)。

输出格式

你的程序应该输出 $n$ 行。第一行应该包含一个整数,等于将所有文件合并为一个文件所需的最小总时间。接下来的每一行对应一次两个文件的合并。第 $i+1$ 行 ($1 \le i \le n-1$) 应该包含两个由单个空格分隔的整数 $k_i, l_i$ ($1 \le k_i < l_i \le n$)。这些数字表示在第 $i$ 步中,Bajtazar 合并了编号为 $k_i$ 和 $l_i$ 的两个文件。新生成的文件获得编号 $k_i$,其长度等于原文件 $k_i$ 和 $l_i$ 的长度之和。

如果存在多种最优的合并顺序,你的程序只需输出其中一种。

样例

输入 1

4
1 2 4 7

输出 1

24
1 2
1 3
1 4

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.