[dp] CF#666(div2) E. Monster Invaders

题目链接

Ziota found a video game called "Monster Invaders".

Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.

For the sake of simplicity, we only consider two different types of monsters and three different types of guns.

Namely, the two types of monsters are:

a normal monster with 1 hp.
a boss with 2 hp.
And the three types of guns are:

Pistol, deals 1 hp in damage to one monster, r1 reloading time
Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r2 reloading time
AWP, instantly kills any monster, r3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.

The levels of the game can be considered as an array a1,a2,…,an, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.

If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1<i<n) are levels i−1 and i+1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n−1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.

In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.

Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.

Input

The first line of the input contains five integers separated by single spaces: n (2≤n≤10^6) — the number of stages, r1,r2,r3 (1≤r1≤r2≤r3≤10^9) — the reload time of the three guns respectively, d (1≤d≤10^9) — the time of moving between adjacent levels.

The second line of the input contains n integers separated by single spaces a_1,a_2,…,a_n (1≤a_i≤10^6,1≤i≤n).

Output

Print one integer, the minimum time to finish the game.

Sample Input

4 1 3 4 3
3 2 5 1

Sample Output

34

Sample Input

4 2 4 4 1
4 5 1 2

Sample Output

31

思路

首先通过观察发现,只可能在最后两层结束。因为如果在其他层结束,不会比在最后两层结束更优。
由于题目条件的规定,我们可以在一层全部打完或者只剩下 boss 的一滴血然后去别的层数打。如果打完了那么就不需要返回,否则处理完别的层后需要返回处理剩下的一滴血。那么可以用 dp[i][0/1] 表示前 i - 1 层已经处理完毕,第 i 层还剩下 0/1 滴血,枚举四种情况进行 dp 的计算就可以得到答案。
注意最后结束的时候不一定在最后一层还可能在倒数第二层,需要额外的计算。

代码

#include <bits/stdc++.h>
using namespace std;
void IO()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}
typedef long long ll;
const int maxn = 1e6 + 10;
const ll inf = 0x3f3f3f3f3f3f3f3f;
ll dp[maxn][2];
ll a[maxn];
ll n, r1, r2, r3, d; //r1 = pistol, r2 = laser, r3 = AWP
 
void solve()
{
    cin >> n >> r1 >> r2 >> r3 >> d;
    for (int i = 0; i <= n; i++)
        dp[i][0] = dp[i][1] = inf;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
 
    dp[1][0] = r1 * a[1] + r3;
    dp[1][1] = min(r1 * (a[1] + 1), r2);
 
    for (int i = 2; i <= n; i++)
    {
        dp[i][0] = min({dp[i][0], dp[i - 1][0] + d + r1 * a[i] + r3});
        dp[i][0] = min({dp[i][0], dp[i - 1][1] + 3 * d + r1 + min({(a[i] + 2) * r1, r2 + r1, a[i] * r1 + r3})});
        dp[i][1] = min({dp[i][1], dp[i - 1][0] + d + min(r1 * (a[i] + 1), r2)});
        dp[i][1] = min({dp[i][1], dp[i - 1][1] + 3 * d + min(r1 * (a[i] + 1), r2) + r1});
        //printf("debug: dp[%d][0]: %lld, dp[%d][1]: %lld\n", i, dp[i][0], i, dp[i][1]);
    }
    ll ans = dp[n][0];
    ans = min({ans, dp[n - 1][1] + 2 * d + r1 + a[n] * r1 + r3});
    cout << ans << '\n';
}
 
int main()
{
    IO();
    int _ = 1;
    //cin >> _;
    while (_--)
        solve();
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。