动态规划 - 杭电acm1087

http://acm.hdu.edu.cn/showproblem.php?pid=1087

import java.util.Scanner;

public class Main {

    static Scanner input = new Scanner(System.in);
    static {

    }

    public static void main(String[] args) {
        int n;
        while ((n = input.nextInt()) != 0) {
            int[] nums = new int[n];
            int max = 0;
            for (int i = 0; i < nums.length; i++) {
                nums[i] = input.nextInt();
                if (nums[i] > max) {
                    max = nums[i];
                }
            }
            int[] result = new int[max + 1];
            for (int i = 0; i < nums.length; i++) {
                int num = nums[i];
                int tmp = 0;
                for (int k = num - 1; k >= 0; k--) {
                    if (tmp < result[k] + num) {
                        tmp = result[k] + num;
                    }
                }
                result[num] = tmp;
            }

            max = 0;
            for (int i : result) {
                if (max < i) {
                    max = i;
                }
            }
            System.out.println(max);
        }
    }

}
    原文作者:一颗北上广的心
    原文地址: https://www.jianshu.com/p/8ab6b37828f4
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞