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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <sstream>

using namespace std;

typedef unsigned long long ull;

const int MAX_CLI = 200 * 1024;

struct input_t {
	int cli_num;
	ull cli_time[MAX_CLI];
	int oven_num;
	unsigned oven_time[MAX_CLI];
} in;

struct queue_t {
	int b;
	int e;
	int cli_num[MAX_CLI];
	unsigned time_left[MAX_CLI];
	inline void clear() {
		b = e = 0;
	}
	inline void push_back(int cli, unsigned time) {
		cli_num[e] = cli;
		time_left[e] = time;
		++e;
	}
	inline bool empty() {
		return b == e;
	}
	inline int size() {
		return b - e;
	}

	inline int front_num() {
		return cli_num[b];
	}
	inline unsigned& front_time() {
		return time_left[b];
	}
	inline void pop_front() {
		++b;
	}
} que;

void read() {
	cin >> in.cli_num >> in.oven_num;
	for (int i = 0; i < in.cli_num; ++i) {
		cin >> in.cli_time[i];
	}
	for (int i = 0; i < in.oven_num; ++i) {
		cin >> in.oven_time[i];
	}
}

void clean() {
	que.clear();
}

void solve(int tc) {
	ull res = 0;
	ull last_time = 0;
	ull available_time;

	for (int i = 0; i < in.cli_num; ++i) {
		while (!que.empty()) {
//			cout << in.cli_time[i] << " " << last_time << endl;
			available_time = in.cli_time[i] - last_time;
			if (available_time > 0) {
				if (available_time >= que.front_time()) {
					last_time += que.front_time();
					res += last_time - in.cli_time[que.front_num()];
//					cout << "current_time: " << last_time << ", client: " << que.front_num() << ", serve time: " << last_time - in.cli_time[que.front_num()] << ", total_res: " << res << endl;
					que.pop_front();
				} else {
					que.front_time() -= available_time;
					last_time = in.cli_time[i];
					break;
				}
			} else {
				break;
			}
		}

		available_time = in.cli_time[i] - last_time;
//		cout << "i: " << i << ", in.cli_time[i]: " << in.cli_time[i] << ", last_time: " << last_time << endl;
		if(available_time < in.oven_time[tc]){
//			cout << "push: " << i << ", time: "<< in.oven_time[tc] - available_time << endl;
			que.push_back(i, in.oven_time[tc] - available_time);
		}
		last_time = in.cli_time[i];
	}

	while (!que.empty()) {
		last_time += que.front_time();
		res += last_time - in.cli_time[que.front_num()];
//		cout << "current_time: " << last_time << ", client: " << que.front_num() << ", serve time: " << last_time - in.cli_time[que.front_num()] << ", total_res: " << res << endl;
		que.pop_front();
	}

	cout << res << endl;
}

int main() {
	ios_base::sync_with_stdio(0);

	read();

	for (int i = 0; i < in.oven_num; ++i) {
		clean();
		solve(i);
	}

	return 0;
}