1046 Shortest Distance (20)(20 分)

#include
#include
using namespace std;
const int maxn = 1e5 + 10;
int num[maxn], n, tot, ans;
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        int x;
        scanf("%d", &x);
        num[i] = num[i - 1] + x;
        tot += x;
    }
    int k;
    scanf("%d", &k);
    while (k--)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        if (x > y)swap(x, y);
        int dis = num[y - 1] - num[x - 1];
        ans = min(dis, tot - dis);
        printf("%d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(1046 Shortest Distance (20)(20 分))