#include <iostream> using namespace std; int main(){ long long n,m; cin >> n >> m; long long cli[n]; long long sto[m]; long long h = 0ll; long long pr = 0ll; long long mx = 0ll; for(int i = 0; i < n; i++) { cin >> h; cli[i] = h - pr; pr = h; if(cli[i] > mx) mx = cli[i]; } for(int j = 0; j < m; j++) cin >> sto[j]; long long res; long long ver; long long ind; res = max(0ll, mx - cli[0]); ver = max(0ll, mx - cli[0]); for(int s = 1; s < n; s++) { if(mx > (cli[s] - ver)) { res += (mx - (cli[s] - ver)); ver = max((mx - (cli[s] - ver)),0ll); ver = max(ver,0ll); } else ver = 0; } ind = res; for(int k = 0; k < m; k++) { if(sto[k] < mx) { res = max(0ll, sto[k] - cli[0]); ver = max(0ll, sto[k] - cli[0]); for(int s = 1; s < n; s++) { if(sto[k] > (cli[s] - ver)) { res += (sto[k] - (cli[s] - ver)); ver = max((sto[k] - (cli[s] - ver)),0ll); ver = max(ver,0ll); } else ver = 0ll; } } else { res = ind + (sto[k] - mx)*(n * (n + 1)) / 2; } cout << res << endl; } return 0; }
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 | #include <iostream> using namespace std; int main(){ long long n,m; cin >> n >> m; long long cli[n]; long long sto[m]; long long h = 0ll; long long pr = 0ll; long long mx = 0ll; for(int i = 0; i < n; i++) { cin >> h; cli[i] = h - pr; pr = h; if(cli[i] > mx) mx = cli[i]; } for(int j = 0; j < m; j++) cin >> sto[j]; long long res; long long ver; long long ind; res = max(0ll, mx - cli[0]); ver = max(0ll, mx - cli[0]); for(int s = 1; s < n; s++) { if(mx > (cli[s] - ver)) { res += (mx - (cli[s] - ver)); ver = max((mx - (cli[s] - ver)),0ll); ver = max(ver,0ll); } else ver = 0; } ind = res; for(int k = 0; k < m; k++) { if(sto[k] < mx) { res = max(0ll, sto[k] - cli[0]); ver = max(0ll, sto[k] - cli[0]); for(int s = 1; s < n; s++) { if(sto[k] > (cli[s] - ver)) { res += (sto[k] - (cli[s] - ver)); ver = max((sto[k] - (cli[s] - ver)),0ll); ver = max(ver,0ll); } else ver = 0ll; } } else { res = ind + (sto[k] - mx)*(n * (n + 1)) / 2; } cout << res << endl; } return 0; } |