-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path168-mukhammadali-and-the-smooth-array.cpp
More file actions
84 lines (73 loc) · 2 KB
/
Copy path168-mukhammadali-and-the-smooth-array.cpp
File metadata and controls
84 lines (73 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Mukhammadali and the Smooth Array [2167G]
* Problem: https://codeforces.com/problemset/problem/2167/G
* Verdict: ACCEPTED Solved: 2025-10-28
* Language: C++23 (GCC 14-64, msys2)
* Runtime: 218 ms Memory: 100 KB
* Tags: data structures, dp
* Author: BidoTeima
* Source: https://codeforces.com/contest/2167/submission/346381698
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 8001;
int a[N],c[N],n;
/*
ll dp[N][N];
ll rec(int idx, int prv){
if(idx == n) return 0;
if(dp[idx][prv] != -1) return dp[idx][prv];
ll ret = 1e18;
if(a[idx] < prv){
ret = rec(idx + 1, prv) + c[idx];
}else{
ret = min(rec(idx + 1, prv) + c[idx], rec(idx + 1, a[idx]));
}
return dp[idx][prv] = ret;
}*/
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
cin >> n;
map<int,int>mp{};
for(int i = 0; i < n; i++){
cin >> a[i];
mp[a[i]];
}
for(int i = 0; i < n; i++){
cin >> c[i];
}
int idx=0;
for(auto&it:mp){
it.second=idx++;
}
for(int i = 0; i < n; i++) a[i] = mp[a[i]];
ll dp[(int)mp.size()];
for(int j = 0; j < (int)mp.size(); j++)dp[j]=1e18;
for(int i = 0; i < a[0]; i++){
dp[i] = c[0];
}
dp[a[0]] = 0;
for(int i = 1; i < n; i++){
ll new_dp[(int)mp.size()];
for(int j = 0; j < (int)mp.size(); j++)new_dp[j]=1e18;
for(int j = 0; j < (int)mp.size(); j++){
new_dp[j] = min(new_dp[j], dp[j] + c[i]);
if(a[i]>=j){
new_dp[a[i]] = min(new_dp[a[i]], dp[j]);
}
}
for(int j = 0; j < (int)mp.size(); j++)dp[j]=new_dp[j];
}
ll mn = 1e18;
for(int j = 0; j < (int)mp.size(); j++)mn=min(mn,dp[j]);
cout << mn << '\n';
}
return 0;
}