第一行输入一个正整数,代表站的数量。
第二行输入个正整数
,前
个数代表顺时针沿着公路走,
站到第
站之间的距离;最后一个正整数代表顺时针沿着公路走,第
站到第 1 站的距离。·
第三行输入两个正整数和
,代表小美的出发地和目的地。
一个正整数,代表小美走的最短距离。
3 1 2 2 2 3
2
3 1 2 2 1 3
2
import java.util.*;
/**
* @author ayj
* @date 2023/9/11
* @Description 美团2024届秋招笔试第一场编程真题
*/
public class Main {
private static final Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
question06();
}
private static void question06() {
int n = sc.nextInt();
long[] preSum = new long[n];
for (int i = 0; i < n; i++) {
preSum[i] = sc.nextLong();
if (i != 0) {
preSum[i] += preSum[i - 1];
}
}
int x = sc.nextInt() - 1;
int y = sc.nextInt() - 1;
if (x > y) {
x = x ^ y;
y = x ^ y;
x = x ^ y;
}
long leftDis = x > 0 ? preSum[y - 1] - preSum[x - 1] : preSum[y - 1];
long rightDis = preSum[n - 1] - preSum[y - 1];
if(x > 0){
rightDis += preSum[x - 1];
}
System.out.println(Math.min(leftDis, rightDis));
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
Long[] a = new Long[n];
for(int i = 0; i < n; i++) {
a[i] = in.nextLong();
}
Long x = in.nextLong() - 1;
Long y = in.nextLong() - 1;
if(x > y) {
Long t = x;
x = y;
y = t;
}
if(x == y) {
System.out.print(0);
return;
}
Long sum1 = Long.valueOf(0);
Long sum2 = Long.valueOf(0);
for(int i = 0; i < n; i++) {
if((x + i) != y) {
sum1 += a[(int) (x + i)];
}else {
break;
}
}
for(int i = 0; i < n; i++) {
if((y + i) % n != x) {
sum2 += a[(int) ((y + i) % n)];
}else {
break;
}
}
System.out.println(Math.min(sum1, sum2));
}
}
}