#abc328g. G - Cut and Reorder
G - Cut and Reorder
Score : points
问题描述
给定两个长度为 的序列: 和 。
你可以对序列 进行任意次数、任意顺序的以下两种操作:
- 将 在任意位置分割并自由重排分割后的序列。每次分割操作会产生一个代价 。更形式化地表述,以代价 来进行操作,取一个长度为 的子序列索引集合 $(i_0,i_1,i_2,\ldots,i_X)\ (0=i_0<i_1<i_2<\cdots<i_X=N)$ 以及该集合的一个排列 (排列自 ),然后按照 的升序,将 替换为 $(A_{i_{p_j-1}+1},A_{i_{p_j-1}+2},\ldots,A_{i_{p_j}})$ 的串联。
- 选择一个整数 以及 中的任意一个元素,并将所选元素的值增加 ,这个操作的代价为 。
通过执行这些操作,找到使 和 相等所需的最小总代价。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are given two sequences of length : and .
You can perform the following two types of operations on the sequence any number of times in any order.
- Split at any positions and freely rearrange the split sequences. Each split position incurs a cost of . More formally, at a cost of , take a sequence of length , $(i_0,i_1,i_2,\ldots,i_X)\ (0=i_0\lt i_1\lt i_2\lt\cdots\lt i_X=N)$, and a permutation of , and replace with the concatenation of $(A_{i_{p_j-1}+1},A_{i_{p_j-1}+2},\ldots,A_{i_{p_j}})$ in ascending order of .
- Choose an integer and any element of , and add to the value of the chosen element, for a cost of .
Find the minimum total cost required to make and equal by performing the operations.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
5 1
3 1 4 1 5
9 2 6 5 3
Sample Output 1
12
For example, you can make and equal by performing the following operations.
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
- Split into and , and swap their order. It costs , and becomes .
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
The total cost incurred is .
It is impossible to make and equal with a total cost of or less, so print .
Sample Input 2
5 1000000000
3 1 4 1 5
9 2 6 5 3
Sample Output 2
15
For example, you can make and equal by performing the following operations.
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
The total cost incurred is .
It is impossible to make and equal with a total cost of or less, so print .
Sample Input 3
22 467772225675200
814424018890229 837987908732596 281175505732576 405797525366223 319378664987871 305374284356649 519144936694626 316916938328237 590332737480143 506785561790072 945769796193819 365498597798550 5386616044591 672368930784037 478017750715806 340276460237787 176509793332130 2734777402752 677509027289850 250325127275409 260270543315523 103584313625431
720386673780641 77160494100361 540947273460639 255177791002759 969333325196025 477751866935037 369600749728569 466236682780196 343161112138696 541310338013515 42740499599240 165778332156355 618106559852784 16582487395877 591851763813728 221861304303645 982850624742022 728669467505250 337968530842725 746724490610504 61587851254728 451153536869240
Sample Output 3
4370668608634071
Note that the input and the answer may not fit into a integer.
update @ 2024/3/10 01:53:41