This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ld long double
#define ll long long
#define int long long
mt19937 rnd(51);
int n, m;
bool connect(int i, int j, int ii, int jj){
    set<pair<int,int>> st;
    while(i < n && j < m){
        st.insert({i, j});
        i++, j++;
    }
    while(ii < n && jj >= 0){
        if (st.find({ii, jj}) != st.end()) return 1;
        ii++, jj--;
    }
    return 0;
}
int take_ans(vector<pair<pair<int,int>, int>> a, vector<pair<pair<int,int>, int>> b){
    vector<pair<int,int>> need(b.size());
    vector<vector<int>> dp(b.size() + 1, vector<int>(a.size() + 1, 1e18));
    for (int j = 0; j < b.size(); j++){
        vector<int> ind;
        for (int i = 0; i < a.size(); i++){
            if (connect(a[i].first.first, a[i].first.second, b[j].first.first, b[j].first.second)){
                ind.pb(i);
            }
        }
        if (ind.size() > 0){
            need[j] = {ind[0], ind.back()};
        }
    }
    dp[0][0] = 0;
    for (int i = 1; i <= b.size(); i++){
        for (int j = 0; j <= a.size(); j++){
            dp[i][j] = min(dp[i][j], b[i - 1].second + (i > 0 ? dp[i - 1][j] : 0));
            int add = (i > 0 ? dp[i - 1][j] : 0);
            for (int f = max(j, need[i - 1].first); f <= need[i - 1].second; f++) add += a[f].second;
            dp[i][max(j, need[i - 1].second)] = min(dp[i][max(j, need[i - 1].second)], add);
        }
    }
    int ans = 1e18;
    for (int i = 0; i <= a.size(); i++) ans = min(ans, dp[(int)(b.size())][i]);
    return ans;
}
signed main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    vector<pair<pair<int,int>, int>> l0, l1, r0, r1;
    int f = 0, s = m - 1;
    for (int i = 0; i < n + m - 1; i++){
        int a;
        cin >> a;
        if ((f + s) & 1){
            l1.pb({{f, s}, a});
        }
        else{
            l0.pb({{f, s}, a});
        }
        if (s > 0) s--;
        else f++;
    }
    f = 0, s = 0;
    for (int i = 0; i < n + m - 1; i++){
        int a;
        cin >> a;
        if ((f + s) & 1){
            r1.pb({{f, s}, a});
        }
        else{
            r0.pb({{f, s}, a});
        }
        if (s + 1 < m) s++;
        else f++;
    }
    cout << take_ans(l0, r0) + take_ans(l1, r1) << endl;
    return 0;
}
Compilation message (stderr)
colouring.cpp: In function 'long long int take_ans(std::vector<std::pair<std::pair<long long int, long long int>, long long int> >, std::vector<std::pair<std::pair<long long int, long long int>, long long int> >)':
colouring.cpp:30:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |     for (int j = 0; j < b.size(); j++){
      |                     ~~^~~~~~~~~~
colouring.cpp:32:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |         for (int i = 0; i < a.size(); i++){
      |                         ~~^~~~~~~~~~
colouring.cpp:42:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for (int i = 1; i <= b.size(); i++){
      |                     ~~^~~~~~~~~~~
colouring.cpp:43:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |         for (int j = 0; j <= a.size(); j++){
      |                         ~~^~~~~~~~~~~
colouring.cpp:51:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for (int i = 0; i <= a.size(); i++) ans = min(ans, dp[(int)(b.size())][i]);
      |                     ~~^~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |