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<int> dp(b.size(), 1e18);
    vector<vector<pair<int,int>>> need(b.size());
    vector<int> add(b.size());
    for (int i = 0; i < a.size(); i++){
        vector<int> ind;
        for (int j = 0; j < b.size(); j++){
            if (connect(a[i].first.first, a[i].first.second, b[j].first.first, b[j].first.second)){
                ind.pb(j);
            }
        }
        if (ind.size() > 0){
            for (int j = ind[0]; j <= ind.back(); j++){
                add[j] += a[i].second;
            }
        }
    }
    for (int i = 0; i < b.size(); i++){
        dp[i] = min(dp[i], min(b[i].second, add[i]) + (i > 0 ? dp[i - 1] : 0));
    }
    return dp.back();
}
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:31: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]
   31 |     for (int i = 0; i < a.size(); i++){
      |                     ~~^~~~~~~~~~
colouring.cpp:33: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]
   33 |         for (int j = 0; j < b.size(); j++){
      |                         ~~^~~~~~~~~~
colouring.cpp:44: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]
   44 |     for (int i = 0; i < 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... |