Problem:
Score : 1000 points

Problem Statement

You are given a permutation p = (p_1, \ldots, p_N) of \{ 1, \ldots, N \}.
You can perform the following two kinds of operations repeatedly in any order:

Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively.

Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively.

Find the minimum total cost required to sort p in ascending order.

Constraints

All values in input are integers.

1 \leq N \leq 5000

1 \leq A, B \leq 10^9

(p_1 \ldots, p_N) is a permutation of \{ 1, \ldots, N \}.

Input

Input is given from Standard Input in the following format:

N A B
p_1 \cdots p_N

Output

Print the minimum total cost required to sort p in ascending order.

Sample Input 1

3 20 30
3 1 2

Sample Output 1

20

Shifting (p_1, p_2, p_3) to the left by one results in p = (1, 2, 3).

Sample Input 2

4 20 30
4 2 3 1

Sample Output 2

50

One possible sequence of operations is as follows:

Shift (p_1, p_2, p_3, p_4) to the left by one. Now we have p = (2, 3, 1, 4).

Shift (p_1, p_2, p_3) to the right by one. Now we have p = (1, 2, 3, 4).

Here, the total cost is 20 + 30 = 50.

Sample Input 3

1 10 10
1

Sample Output 3

0

Sample Input 4

4 1000000000 1000000000
4 3 2 1

Sample Output 4

3000000000

Sample Input 5

9 40 50
5 3 4 7 6 1 2 9 8

Sample Output 5

220

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <iostream>
#include <random>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include <fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;

using ll = long long;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using qll = queue<ll>;
using vb = vector<bool>;
using mll = map<ll, ll>;
using sll = stack<ll>;
#define REP(i,n) for(ll i(0);(i)<(n);(i)++)
#define rep(i,n) for(ll i(0);(i)<(n);(i)++)
#define ALL(a) a.begin(), a.end()
#define enld endl //* missspell check
const ll INF = 1LL << 59;


struct edgeForFlow{ll to, cap, rev; }; //! cap: may change; rev: pointer in G[to]

void addEdgeForFlow(vector<vector<edgeForFlow>> &G, ll from, ll to, ll cap){
    G[from].push_back((edgeForFlow){ to, cap,  (ll) G[to].size()});
    G[to].push_back((edgeForFlow) { from, 0, (ll) G[from].size()-1});
}

ll dfsFordFulkson(vector<vector<edgeForFlow>> &G, vb &checked, ll v, ll t, ll f){
    //* v: current vertex, t: sink, f: DELTA of this path. (No need for source)
    if(v == t) return f;
    checked[v] = true;
    REP(i, G[v].size()){
        edgeForFlow &e = G[v][i];
        if(!checked[e.to] && e.cap > 0){
            ll d = dfsFordFulkson(G, checked, e.to, t, min(f, e.cap));
            if(d > 0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0; //* if no valid outgoing edges
}

ll maxFlowFordFulkson(vector<vector<edgeForFlow>> &G, ll s, ll t){
    vb checked(G.size());
    ll flow = 0;
    for(;;){
        fill(ALL(checked), false);
        ll f = dfsFordFulkson(G, checked, s, t, INF);
        if(f == 0) 
            return flow;
        flow += f;
    }
}

void bfsDinic(vector<vector<edgeForFlow>> &G, vll &level, ll s){
    fill(ALL(level), -1);
    queue<ll> que;
    level[s] = 0;
    que.push(s);
    while(!que.empty()){
        ll v = que.front(); que.pop();
        for(ll i=0; i< G[v].size(); i++){
            edgeForFlow &e = G[v][i];
            if(e.cap > 0 && level[e.to] < 0){
                level[e.to] = level[v] + 1;
                que.push(e.to);
            }
        }
    }
}

ll dfsDinic(vector<vector<edgeForFlow>> &G, vll &level, vll &iter, ll v, ll t, ll f){
    //? iter: record where have been searched?
    //* v: current vertex, t: sink, f: DELTA of this path. (No need for source)
    if(v == t) return f;
    for( ll &i = iter[v]; i < G[v].size(); i++){
        edgeForFlow &e = G[v][i];
        if( e.cap > 0 && level[v] < level[e.to]){
            ll d = dfsDinic(G, level, iter, e.to, t, min(f, e.cap));
            if(d > 0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0; //* if no valid outgoing edges
}

ll maxFlowDinic(vector<vector<edgeForFlow>> &G, ll s, ll t){
    vll level(G.size()), iter(G.size());
    ll flow = 0;
    for(;;){
        bfsDinic(G, level, s);
        if(level[t] < 0)//* sink t is not reachable from s on current residual graph
            return flow;
        fill(ALL(iter), 0);
        ll f;
        while((f = dfsDinic(G, level, iter, s, t, INF)) > 0){
            flow += f;
        }
    }
}

void solve(long long N, long long A, long long B, std::vector<long long> p){
    vector<vector<edgeForFlow>> G(2*N+2);
    ll s = 2*N, t = 2*N+1;
    REP(i, N){
        addEdgeForFlow(G, s, i, A);
        addEdgeForFlow(G, i+N, t, B);
    }
    for(ll i=0; i< N; i++){
        for(ll j = i+1; j < N; j++){
            ll u = p[i], v = p[j];
            if(p[i] > p[j]) addEdgeForFlow(G, u, v+N, INF);
        }
    }
    ll ans = maxFlowDinic(G, s, t);
    cout<<ans<<endl;
}

int main(){
    long long N;
    scanf("%lld",&N);
    long long A;
    scanf("%lld",&A);
    long long B;
    scanf("%lld",&B);
    std::vector<long long> p(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&p[i]);
        p[i]--;
    }
    solve(N, A, B, std::move(p));
    return 0;
}