//
// Created by CyIce on 2021/2/25.
//
#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAXN = 100010;
// 记录节点1到节点i的距离,固定一个方向
int dis[MAXN] = {0};
int total = 0;
int distance(int a, int b) {
if (a > b) swap(a, b);
int ans = dis[b - 1] - dis[a - 1];
if (total - ans < ans) ans = total - ans;
return ans;
}
int main() {
int n, m, d;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &d);
dis[i + 1] = dis[i] + d;
total += d;
}
scanf("%d", &m);
int a, b;
for (int i = 0; i < m; ++i) {
scanf("%d%d", &a, &b);
printf("%d\n", distance(a, b));
}
return 0;
}
1046 Shortest Distance
最新推荐文章于 2022-01-14 22:37:33 发布