CF1428G2.Lucky Numbers (Hard Version)

NOI/NOI+/CTSC

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is the hard version of the problem. The only difference is in the constraint on qq. You can make hacks only if all versions of the problem are solved.

Zookeeper has been teaching his qq sheep how to write and how to add. The ii-th sheep has to write exactly kk non-negative integers with the sum nin_i.

Strangely, sheep have superstitions about digits and believe that the digits 33, 66, and 99 are lucky. To them, the fortune of a number depends on the decimal representation of the number; the fortune of a number is equal to the sum of fortunes of its digits, and the fortune of a digit depends on its value and position and can be described by the following table. For example, the number 319319 has fortune F2+3F0F_{2} + 3F_{0}.

Each sheep wants to maximize the sum of fortune among all its kk written integers. Can you help them?

这是该问题的困难版本。唯一的区别在于对 qq 的约束条件。只有当该问题的所有版本均被解决后,才允许进行 hack。

动物园园长一直在教他的 qq 只绵羊如何书写和加法运算。第 ii 只绵羊必须恰好写出 kk 个非负整数,且这些整数之和为 nin_i。

奇怪的是,绵羊对数字怀有迷信,并认为数字 33、66 和 99 是幸运数字。对它们而言,一个数的“运势”取决于该数的十进制表示;一个数的运势等于其各位数字运势之和,而每位数字的运势则取决于该数字的数值及其所在位置,具体可由下表描述。例如,数字 319319 的运势为 F2+3F0F_{2} + 3F_{0}。

每只绵羊都希望最大化其所写的 kk 个整数的总运势。你能帮助它们吗?

输入格式

The first line contains a single integer kk (1≤k≤9999991 \leq k \leq 999999): the number of numbers each sheep has to write.

The next line contains six integers F0F_0, F1F_1, F2F_2, F3F_3, F4F_4, F5F_5 (1≤Fi≤1091 \leq F_i \leq 10^9): the fortune assigned to each digit.

The next line contains a single integer qq (1≤q≤100 0001 \leq q \leq 100\,000): the number of sheep.

Each of the next qq lines contains a single integer nin_i (1≤ni≤9999991 \leq n_i \leq 999999): the sum of numbers that ii-th sheep has to write.

第一行包含一个整数 kk(1≤k≤9999991 \leq k \leq 999999):每只绵羊需要写出的数字个数。

下一行包含六个整数 F0F_0, F1F_1, F2F_2, F3F_3, F4F_4, F5F_5(1≤Fi≤1091 \leq F_i \leq 10^9):分别对应数字 00 至 55 的幸运值。

再下一行包含一个整数 qq(1≤q≤100 0001 \leq q \leq 100\,000):绵羊的数量。

接下来的 qq 行,每行包含一个整数 nin_i(1≤ni≤9999991 \leq n_i \leq 999999):第 ii 只绵羊需要写出的所有数字之和。

输出格式

Print qq lines, where the ii-th line contains the maximum sum of fortune of all numbers of the ii-th sheep.

输出 qq 行,其中第 ii 行包含第 ii 只羊的所有数字的运势之和的最大值。

输入输出样例

  • 输入#1

    3
    1 2 3 4 5 6
    2
    57
    63

    输出#1

    11
    8

说明/提示

In the first test case, 57=9+9+3957 = 9 + 9 + 39. The three 99's contribute 1⋅31 \cdot 3 and 33 at the tens position contributes 2⋅12 \cdot 1. Hence the sum of fortune is 1111.

In the second test case, 63=35+19+963 = 35 + 19 + 9. The sum of fortune is 88.

在第一个测试用例中,57=9+9+3957 = 9 + 9 + 39。三个 99 在个位上,贡献 1⋅31 \cdot 3;3939 中的 33 在十位上,贡献 2⋅12 \cdot 1。因此幸运值之和为 1111。

在第二个测试用例中,63=35+19+963 = 35 + 19 + 9。幸运值之和为 88。

输入解题思路,AI测评打分。不知道怎么写?

首页