#include<cstdio> using namespace std; int n, m; long long int tab [200007]; long long int f(long long int x){ long long int wsk = 0LL, w = 0LL; for(int i = 1;i <= n; i++){ if(wsk < tab[i] - x){ wsk = tab[i]; continue; } long long y = 0LL; if(wsk <= tab[i]){ y = x - (tab[i] - wsk); } else{ y = wsk - tab[i] + x; } //printf("%d %d\n",i,y); w += y; wsk = tab[i] + y; } return w; } int main(){ scanf("%d %d",&n,&m); for(int i = 1;i <= n; i++){ scanf("%lld",&tab[i]); } for(int i = 1;i <= m; i++){ int d; scanf("%d",&d); printf("%lld\n",f((long long)d)); } 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 | #include<cstdio> using namespace std; int n, m; long long int tab [200007]; long long int f(long long int x){ long long int wsk = 0LL, w = 0LL; for(int i = 1;i <= n; i++){ if(wsk < tab[i] - x){ wsk = tab[i]; continue; } long long y = 0LL; if(wsk <= tab[i]){ y = x - (tab[i] - wsk); } else{ y = wsk - tab[i] + x; } //printf("%d %d\n",i,y); w += y; wsk = tab[i] + y; } return w; } int main(){ scanf("%d %d",&n,&m); for(int i = 1;i <= n; i++){ scanf("%lld",&tab[i]); } for(int i = 1;i <= m; i++){ int d; scanf("%d",&d); printf("%lld\n",f((long long)d)); } return 0; } |