1625C - Road Optimization - CodeForces Solution


dp *1700

Please click on ads to support us..

Python Code:

n,l,k=map(int,input().split())
a=list(map(int,input().split()))+[l]
b=list(map(int,input().split()))+[0]
dp={}
def dfs(i,k,p):
    if (i,k,p) in dp:
        return dp[(i,k,p)]
    if i==n:
        return 0
    res=float("inf")
    if k>0 and b[i]>p:
        res=min(res,p*(a[i+1]-a[i])+dfs(i+1,k-1,p))
    res=min(res,b[i]*(a[i+1]-a[i])+dfs(i+1,k,b[i]))
    dp[(i,k,p)]=res
    return res
print(a[1]*b[0]+dfs(1,k,b[0]))

C++ Code:

#include<bits/stdc++.h>
#define LL long long
#define pb push_back
#define int long long
#define all(x) x.begin(), x.end()
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;

const int N = 5e2 + 5, mod = 1e9 + 7;
int tc, n, m, k, ans, a[N], dp[N][N], d[N], l;
string s, t;
bool flag, vis[N], pr[N];

int32_t main(){
    FASTER;
//    freopen("", "r", stdin);

    tc=1;
//    cin>>tc;
    while(tc--){
        cin>>n>>l>>k;
        d[n+1] = l;

        for(int i=1; i<=n; i++)cin>>d[i];
        for(int i=1; i<=n; i++)cin>>a[i];

        for(int i=0; i<=n+1; i++)
            for(int j=0; j<=k; j++)
                dp[i][j] = 1e10;

        dp[1][0] = a[1] * d[2];
        for(int i=2; i<=n+1; i++){
            for(int j=0; j<=min(i-2, k); j++){
                for(int lst=i-j-1; lst<i; lst++)
                    dp[i][j] = min(dp[i][j], dp[lst][j-i+lst+1] + (d[i] - d[lst+1]) * a[lst] + (d[i+1] - d[i]) * a[i]);
            }
        }
        ans = 1e10;
        for(int i=0; i<=k; i++)ans = min(ans, dp[n+1][i]);
        
        cout<<ans<<endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness
1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence
21D - Traveling Graph
1559B - Mocha and Red and Blue
1579C - Ticks
268B - Buttons
898A - Rounding
1372B - Omkar and Last Class of Math
1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher