#include<bits/stdc++.h> #define MAX 200000 using namespace std; unsigned long long max2(long long a, long long b) { return a > b ? a : b; } unsigned long long piece[MAX+1]; unsigned long long chwile[MAX+1]; unsigned long long result[MAX+1]; int main() { int n, m; cin >> n >> m; unsigned long long suma_chwil = 0; for(int i = 0; i < n; i++) { cin >> chwile[i]; suma_chwil += chwile[i]; } for(int i = 0; i < m; i++) { cin >> piece[i]; } for(int i = 0; i < m; i++) { unsigned long long suma = 0; result[0] = max2(0, chwile[0] - piece[i]); for(int j = 1; j <=n; j++) { result[j] =max2(result[j-1] + piece[i], chwile[j] - piece[i]); } for(int j = 0; j < n; j++) suma += result[j]; cout << suma + n*piece[i] - suma_chwil << endl; } }
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 | #include<bits/stdc++.h> #define MAX 200000 using namespace std; unsigned long long max2(long long a, long long b) { return a > b ? a : b; } unsigned long long piece[MAX+1]; unsigned long long chwile[MAX+1]; unsigned long long result[MAX+1]; int main() { int n, m; cin >> n >> m; unsigned long long suma_chwil = 0; for(int i = 0; i < n; i++) { cin >> chwile[i]; suma_chwil += chwile[i]; } for(int i = 0; i < m; i++) { cin >> piece[i]; } for(int i = 0; i < m; i++) { unsigned long long suma = 0; result[0] = max2(0, chwile[0] - piece[i]); for(int j = 1; j <=n; j++) { result[j] =max2(result[j-1] + piece[i], chwile[j] - piece[i]); } for(int j = 0; j < n; j++) suma += result[j]; cout << suma + n*piece[i] - suma_chwil << endl; } } |