#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2007;
int prefA[MAXN];
int prefB[MAXN];
int DP[MAXN][MAXN];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, x;
cin >> n >> m;
//sumy prefiksowe obu talerzy
for(int i = 1; i <= n; i++){
cin >> x;
prefA[i] = prefA[i-1]+x;
}
for(int i = 1; i <= m; i++){
cin >> x;
prefB[i] = prefB[i-1]+x;
}
//dynamik
//bierzemy wszystkie nalesniki z obu stosow i odejmujemy najgorszy wynik jesli jeden z gornych nalesnikow jest zabrany
for(int i = 0; i <= n; i++){
for(int j = 0; j <= m; j++){
if(i==0 && j==0){
DP[i][j] = 0;
}else if(i == 0){
DP[i][j] = prefA[i] + prefB[j] - DP[i][j-1];
}else if(j == 0){
DP[i][j] = prefA[i] + prefB[j] - DP[i-1][j];
}else{
DP[i][j] = prefA[i] + prefB[j] - min(DP[i-1][j], DP[i][j-1]);
}
cout << i << " " << j << " " << DP[i][j] << "\n";
}
}
cout << DP[n][m] << "\n";
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1BWE4gPSAyMDA3OwoKaW50IHByZWZBW01BWE5dOwppbnQgcHJlZkJbTUFYTl07CmludCBEUFtNQVhOXVtNQVhOXTsKCgppbnQgbWFpbigpewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsKICAgIGNpbi50aWUoMCk7CiAgICBjb3V0LnRpZSgwKTsKICAgIGludCBuLCBtLCB4OwogICAgY2luID4+IG4gPj4gbTsKICAgIC8vc3VteSBwcmVmaWtzb3dlIG9idSB0YWxlcnp5CiAgICBmb3IoaW50IGkgPSAxOyBpIDw9IG47IGkrKyl7CiAgICAgICAgY2luID4+IHg7CiAgICAgICAgcHJlZkFbaV0gPSBwcmVmQVtpLTFdK3g7CiAgICB9CiAgICBmb3IoaW50IGkgPSAxOyBpIDw9IG07IGkrKyl7CiAgICAgICAgY2luID4+IHg7CiAgICAgICAgcHJlZkJbaV0gPSBwcmVmQltpLTFdK3g7CiAgICB9CgogICAgLy9keW5hbWlrCiAgICAvL2JpZXJ6ZW15IHdzenlzdGtpZSBuYWxlc25pa2kgeiBvYnUgc3Rvc293IGkgb2Rlam11amVteSBuYWpnb3Jzenkgd3luaWsgamVzbGkgamVkZW4geiBnb3JueWNoIG5hbGVzbmlrb3cgamVzdCB6YWJyYW55CiAgICBmb3IoaW50IGkgPSAwOyBpIDw9IG47IGkrKyl7CiAgICAgICAgZm9yKGludCBqID0gMDsgaiA8PSBtOyBqKyspewogICAgICAgICAgICBpZihpPT0wICYmIGo9PTApewogICAgICAgICAgICAgICAgRFBbaV1bal0gPSAwOwogICAgICAgICAgICB9ZWxzZSBpZihpID09IDApewogICAgICAgICAgICAgICAgRFBbaV1bal0gPSBwcmVmQVtpXSArIHByZWZCW2pdIC0gRFBbaV1bai0xXTsKICAgICAgICAgICAgfWVsc2UgaWYoaiA9PSAwKXsKICAgICAgICAgICAgICAgIERQW2ldW2pdID0gcHJlZkFbaV0gKyBwcmVmQltqXSAtIERQW2ktMV1bal07CiAgICAgICAgICAgIH1lbHNlewogICAgICAgICAgICAgICAgRFBbaV1bal0gPSBwcmVmQVtpXSArIHByZWZCW2pdIC0gbWluKERQW2ktMV1bal0sIERQW2ldW2otMV0pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGNvdXQgPDwgaSA8PCAiICIgPDwgaiA8PCAiICIgPDwgRFBbaV1bal0gPDwgIlxuIjsKICAgICAgICB9CiAgICB9CgogICAgY291dCA8PCBEUFtuXVttXSA8PCAiXG4iOwogICAgcmV0dXJuIDA7Cn0=