#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <unordered_map>
#include <iterator>
#include <stack>
#include <unordered_set>
#include <iomanip>
#include <ctime>
#include <cstring>
#include <bitset>

#include <random>
#define all(x) (x).begin(), (x).end()


using namespace std;
void dout() { cerr << endl; }
typedef long long ll;
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
    cerr << H << ' ';
    dout(T...);
}

#define debug(x) cout << #x " = " << (x) << endl

using ll = long long;
using pii = pair<ll, ll>;
using ld = long double;
using pll = pair<long long, long long>;

template <typename T>
void do_uniq(vector<T> vec) {
    sort(all(vec));
    vec.resize(unique(all(vec)) - vec.begin());
}

//#define solvsh
//#define multi
#ifdef solvsh

const int MAXN = 2e5 + 20;

int per[MAXN];
ll costs[MAXN];

ll tree[MAXN];
ll inp[MAXN];
int where[MAXN];

void build(int ver, int tl, int tr) {
    if (tl == tr) {
        tree[ver] = inp[tl];
    } else {

        int mid = (tl + tr) >> 1;

        build(2 * ver, tl, mid);
        build(2 * ver + 1, mid + 1, tr);

        tree[ver] = min(tree[2 * ver], tree[2  *ver + 1]);
    }
}

ll adds[4 * MAXN];

void upd(int v, int tl, int tr, int l, int r, ll add){
    if (l > r)
        return;
    if (tl == l && tr == r){
        adds[v] += add;
        return;
    }

    auto mid = (tl + tr) >> 1;

    upd(2 * v, tl, mid, l, min(mid, r), add);
    upd(2 * v + 1, mid + 1, tr, max(mid + 1, l), r, add);

    tree[v] = min(tree[2 * v], tree[2 * v  +1]) + adds[v];
}

ll get_min(){
    return tree[1];
}


void solve() {
    int n;
    cin >> n;

    for (int i = 0; i < n; ++i) {
        cin >> per[i];
        where[per[i]] = i;
    }

    ll sum = 0;
    for (int i = 0; i < n; ++i) {
        cin >> costs[i];
        sum += costs[i];
    }

    for (int i = 1; i <= n; ++i) {
        inp[i] = inp[i - 1] + costs[where[i]];
    }
    build(1, 1, n);

    ll answr = 1e18;

    for (int i = 0; i < n; ++i) {

        int val = per[i];

        upd(1, 1, n, 1, val - 1, costs[i]);
        upd(1, 1, n, val, n, -costs[i]);

        answr = min(answr, get_min());
    }

    cout << answr;
}

#else

const int MAXN = 2e5 + 9;

ll mod = 998244353;

ll ml(ll a, ll b){
    return (a * b) % mod;
}

ll bin_pow(ll b, ll p){
    if (p == 0)
        return 1;

    if (p % 2 == 1) {
        return ml(b, bin_pow(b, p - 1));
    }
    ll val = bin_pow(b, p >> 1);
    return ml(val, val);
}

ll get_back(ll elem){
    return bin_pow(elem, mod - 2);
}

ll facts[MAXN];

ll backs[MAXN];

ll add(ll a, ll b){
    return (a + b) % mod;
}


ll nCr(int n, int r) {
    return ml(facts[n],
              get_back(
                      ml(facts[r],
                         facts[n - r])));
}

void solve() {
    ll n, m, k;
    cin >> n >> m >> k;

    facts[0] = 1;

    for (int i = 1; i <= n; ++ i) {
        facts[i ] = ml(facts[ i - 1], i);
        backs[i] = get_back(facts[i]);
    }

    ll answr = 0;


    for (int i = 0;i <= k; ++ i) {
        ll curr = m;

        ll cnts = nCr(n - 1, i);

        curr = ml(curr, cnts);

        ll nums = bin_pow(m - 1, n - 1 - i);
        curr = ml(curr, nums);

        answr = add(answr, curr);
    }

    cout << answr;
}

#endif

void multisolve() {

    int t;
    cin >> t;

    int i = 1;
    while (t--) {
        cout << "Case #" << (i++) << ": ";
        solve();
        cout << "\n";
    }
}


#define int int
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    cout << fixed << setprecision(20);


#ifdef multi
    multisolve();
#else
    solve();
    //    gen();
#endif

}


Measured memory_bytes: 6744