A14064 | Mathematical Expression
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Barbara was late for her math class so as a punishment the teacher made her solve the task on a sheet of paper. Barbara looked at the sheet of paper and only saw $n$ numbers $a_1, a_2, \ldots, a_n$ without any mathematical symbols. The teacher explained to Barbara that she has to place the available symbols between the numbers in a way that would make the resulting expression's value as large as possible. To find out which symbols were available the teacher has given Barbara a string $s$ which contained that information.
It's easy to notice that Barbara has to place $n - 1$ symbols between numbers in total. The expression must start with a number and all symbols must be allowed (i.e. included in $s$ ). Note that multiplication takes precedence over addition or subtraction, addition and subtraction have the same priority and performed from left to right. Help Barbara and create the required expression!
It's easy to notice that Barbara has to place $n - 1$ symbols between numbers in total. The expression must start with a number and all symbols must be allowed (i.e. included in $s$ ). Note that multiplication takes precedence over addition or subtraction, addition and subtraction have the same priority and performed from left to right. Help Barbara and create the required expression!
输入格式
The first line of the input contains a single integer $n$ ( $1 \le n \le 10^5$ ) — the amount of numbers on the paper.
The second line of the input contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $0 \le a_i \le 9$ ), where $a_i$ is the $i$ -th element of $a$ .
The third line of the input contains the string $s$ ( $1 \le |s| \le 3$ ) — symbols allowed in the expression. It is guaranteed that the string may only consist of symbols "-", "+" and "\*". It is also guaranteed that all symbols in the string are distinct.
The second line of the input contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $0 \le a_i \le 9$ ), where $a_i$ is the $i$ -th element of $a$ .
The third line of the input contains the string $s$ ( $1 \le |s| \le 3$ ) — symbols allowed in the expression. It is guaranteed that the string may only consist of symbols "-", "+" and "\*". It is also guaranteed that all symbols in the string are distinct.
输出格式
Print $n$ numbers separated by $n - 1$ symbols — a mathematical expression with the greatest result. If there are multiple equally valid results — output any one of them.
输入输出样例
输入 #1
3 2 2 0 +-*
输出 #1
2*2-0
输入 #2
4 2 1 1 2 +*
输出 #2
2+1+1+2
The following answers also fit the first example: "2+2+0", "2+2-0", "2\*2+0".
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted