PAT (Advanced) 1046. Shortest Distance (20)

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int n;
	cin >> n;
	int tot = 0;
	vector<int> dis(n+1);
	dis[0] = 0;
	int tmp;
	for (int i = 1; i <= n; i++)
	{
		cin >> tmp;
		dis[i] = dis[i - 1] + tmp;
		tot += tmp;
	}
	int m;
	cin >> m;
	int begin, end;
	int sum;
	for (int i = 0; i < m; i++)
	{
		sum = 0;
		cin >> begin >> end;
		sum = abs(dis[begin - 1] - dis[end - 1]);
		if (sum << 1 < tot)
			cout << sum << endl;
		else
			cout << tot - sum << endl;
	}
	return 0;
}

你可能感兴趣的:(PAT (Advanced) 1046. Shortest Distance (20))