招行信用卡中心笔试大数据第一题
同输入输出问题。。。。第一题两个测试用例全过了,提交为0%
寻求热心小伙伴指点。感谢!!
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); List<Long> out = new ArrayList<>(); while (sc.hasNextInt()) { int n = sc.nextInt(); Long[] a = new Long[n + 1]; Long[] b = new Long[n]; for (int i = 0; i <= n; i++) { a[i] = sc.nextLong(); } for (int i = 0; i < n; i++) { b[i] = sc.nextLong(); } out.add(solution(n, a, b)); } for (Long line : out) { System.out.println(line); } } private static Long solution(int n, Long[] a, Long[] b) { Long out = 0l; for (int i = 0; i < n; i++) { Long ai = a[i]; Long next = a[i + 1]; Long bi = b[i]; if (bi >= ai) { out += ai; bi = bi - ai; if (bi >= next) { out += next; a[i + 1] = 0l; } else { out += bi; a[i + 1] = a[i + 1] - bi; } } } return out; } }