题解 | #牛牛吃草#

牛牛吃草

https://www.nowcoder.com/practice/f05254f070944ff792c0dfefabd94fec

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] f = new int[n];
        int[] w = new int[n];
        int[] a = new int[n];
        int max = 0;
        for(int i = 0; i < n; i++) w[i] = in.nextInt();
        for(int i = 0; i < n; i++) a[i] = in.nextInt();

        for (int i = 0; i < n; i++) {
            // 第一个格子 i == 0
            int maxC = w[i];
            for (int j = 0; j < i; j++) {
                // 判断是否可以走到
                if (a[j] > 0 && (i - j) % a[j] == 0) {
                    // 可以走到
                    int cur = f[j] + w[i];
                    if (cur > maxC) {
                        maxC = cur;
                    }
                }
            }

            f[i] = maxC;
            if (maxC > max) {
                max = maxC;
            }
        }

        System.out.println(max);
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务