Language: C++

// mainメソッドを含むKPクラスを書く
import java.util.Scanner;
import java.io.*;
   
class Main
{
    public static void main(String[] args)
    {
        // ファイルの読み込み
        Scanner reader = new Scanner(System.in);
        String firstData[] = {};
        // まず、1行目を特別なものとして読み込む
        firstData = reader.nextLine().split(" ", -1);
   
        // それをもとに各種フィールドを設定する
        int answer; // 品物の数size，ナップサックの容量(重量の上限)limitWeight，答えanswer
        int size = 0;
        size = Integer.valueOf(firstData[0]);
        int limitWeight = Integer.valueOf(firstData[1]);
   
        String nextLine = null;
        String nextLineSplitted[] = new String[2];
        int weight[] = new int[size];
        int value[] = new int[size];
   
        int counter = 0;
        for(int i=0;i<size;i++)
        {
            nextLine = reader.nextLine();
           
            // 重さ 価値の順に入る
            nextLineSplitted = nextLine.split(" ", -1);
            weight[counter] = Integer.valueOf(nextLineSplitted[1]);
            value[counter] = Integer.valueOf(nextLineSplitted[0]);
            counter++;
        }        // 必要に応じて内容の確認
        Solver s = new Solver(size, limitWeight, weight, value);
        answer = s.solve();
        System.out.println(answer);
    }
}
   
// Solverクラスを書く
class Solver
{
    int answer = 0;
    int size, limitWeight;
    int[] weight, value;
    // 縦、横、内容物
    int[][][] answerArray;
   
    int bestWeight = 0;
   
    // コンストラクタ
    public Solver(int size, int limitWeight, int[] weight, int[] value)
    {
        this.size = size;
        this.limitWeight = limitWeight;
        this.weight = weight;
        this.value = value;
        answerArray = new int[size][limitWeight + 1][2];
    }
   
    int solve()
    {
        if (size == 0)
        {
            System.out.println("入力がありません。");
            return 0;
        }
        if (limitWeight == 0)
        {
            System.out.println("限界重量が不正な値です");
            return 0;
        }
        // 正常な値と仮定する
        // 求めたいのは「組み合わせ」
        // 二次元配列で保存する必要性はない
   
        // 一つずつ比較する数を増やしていく
        for (int i = 0; i < size; i++)
        {
            int checkingWeight = weight[i];
            int checkingValue = value[i];
            if (checkingWeight > limitWeight)
            {
                continue;
            }
            // checing要素を根源要素としてAnserArrayに保存
            answerArray[i][checkingWeight][0] = checkingWeight;
            answerArray[i][checkingWeight][1] = checkingValue;
            if(answer<checkingValue)
            {
                answer = checkingValue;
                bestWeight = checkingWeight;
            }
   
            // Answer候補一つ一つに対して処理を行う
            for (int j = 0; j < i; j++)
            {
                for (int k = 0; k < limitWeight; k++)
                {
                    // answerArray[j][k]にはj行k列目の要素が入っている
                    // 各要素ごとに2つの要素weightとvalueが格納されている
   
                    // 中身が空ならAnswer候補はないものとしてスルーする
                    if (answerArray[j][k][0] == 0 && answerArray[j][k][1] == 0)
                    {
                        continue;
                    } else
                    {
                        try
                        {
                            int tmpWeight = answerArray[j][k][0] + checkingWeight;
                            int tmpValue = answerArray[j][k][1] + checkingValue;
   
                            // tmpWeight<=limitWeight
   
                            if (tmpWeight <= limitWeight)
                            {
                                if (answer < tmpValue)
                                {
                                    answer = tmpValue;
                                    bestWeight = tmpWeight;
                                }
                                answerArray[i][tmpWeight][0] = tmpWeight;
                                if (answerArray[i][tmpWeight][1] < tmpValue)
                                {
                                    answerArray[i][tmpWeight][1] = tmpValue;
                                }
                            }
                        } catch (ArrayIndexOutOfBoundsException e)
                        {
                            System.out.println("Solver:anserArrayに対するアクセス違反");
                            e.printStackTrace();
                            System.exit(-1);
                        }
                    }
                }
            }
         }
        return answer;
    }
}


Compiler output:
a.cc:2:1: error: 'import' does not name a type
    2 | import java.util.Scanner;
      | ^~~~~~
a.cc:2:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:3:1: error: 'import' does not name a type
    3 | import java.io.*;
      | ^~~~~~
a.cc:3:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:7:11: error: expected ':' before 'static'
    7 |     public static void main(String[] args)
      |           ^~~~~~~
      |           :
a.cc:7:29: error: 'String' has not been declared
    7 |     public static void main(String[] args)
      |                             ^~~~~~
a.cc:7:38: error: expected ',' or '...' before 'args'
    7 |     public static void main(String[] args)
      |                                      ^~~~
a.cc:41:2: error: expected ';' after class definition
   41 | }
      |  ^
      |  ;
a.cc: In static member function 'static void Main::main(int*)':
a.cc:10:9: error: 'Scanner' was not declared in this scope
   10 |         Scanner reader = new Scanner(System.in);
      |         ^~~~~~~
a.cc:11:9: error: 'String' was not declared in this scope
   11 |         String firstData[] = {};
      |         ^~~~~~
a.cc:13:9: error: 'firstData' was not declared in this scope
   13 |         firstData = reader.nextLine().split(" ", -1);
      |         ^~~~~~~~~
a.cc:13:21: error: 'reader' was not declared in this scope
   13 |         firstData = reader.nextLine().split(" ", -1);
      |                     ^~~~~~
a.cc:18:16: error: 'Integer' was not declared in this scope
   18 |         size = Integer.valueOf(firstData[0]);
      |                ^~~~~~~
a.cc:21:16: error: expected ';' before 'nextLine'
   21 |         String nextLine = null;
      |                ^~~~~~~~
a.cc:22:16: error: expected ';' before 'nextLineSplitted'
   22 |         String nextLineSplitted[] = new String[2];
      |                ^~~~~~~~~~~~~~~~
a.cc:23:24: error: initializer fails to determine size of 'weight'
   23 |         int weight[] = new int[size];
      |                        ^~~~~~~~~~~~~
a.cc:23:24: error: array must be initialized with a brace-enclosed initializer
a.cc:24:23: error: initializer fails to determine size of 'value'
   24 |         int value[] = new int[size];
      |                       ^~~~~~~~~~~~~
a.cc:24:23: error: array must be initialized with a brace-enclosed initializer
a.cc:29:13: error: 'nextLine' was not declared in this scope
   29 |             nextLine = reader.nextLine();
      |             ^~~~~~~~
a.cc:32:13: error: 'nextLineSplitted' was not declared in this scope
   32 |             nextLineSplitted = nextLine.split(" ", -1);
      |             ^~~~~~~~~~~~~~~~
a.cc:37:9: error: 'Solver' was not declared in this scope
   37 |         Solver s = new Solver(size, limitWeight, weight, value);
      |         ^~~~~~
a.cc:38:18: error: 's' was not declared in this scope
   38 |         answer = s.solve();
      |                  ^
a.cc:39:9: error: 'System' was not declared in this scope
   39 |         System.out.println(answer);
      |         ^~~~~~
a.cc: At global scope:
a.cc:48:8: error: expected unqualified-id before '[' token
   48 |     int[] weight, value;
      |        ^
a.cc:50:8: error: expected unqualified-id before '[' token
   50 |     int[][][] answerArray;
      |        ^
a.cc:55:11: error: expected ':' before 'Solver'
   55 |     public Solver(int size, int limitWeight, int[] weight, int[] value)
      |           ^~~~~~~
      |           :
a.cc:55:52: error: expected ',' or '...' before 'weight'
   55 |     public Solver(int size, int limitWeight, int[] weight, int[] value)
      |                                                    ^~~~~~
a.cc:144:2: error: expected ';' after class definition
  144 | }
      |  ^
      |  ;
a.cc: In constructor 'Solver::Solver(int, int, int*)':
a.cc:57:14: error: request for member 'size' in '(Solver*)this', which is of pointer type 'Solver*' (maybe you meant to use '->' ?)
   57 |         this.size = size;
      |              ^~~~
a.cc:58:14: error: request for member 'limitWeight' in '(Solver*)this', which is of pointer type 'Solver*' (maybe you meant to use '->' ?)
   58 |         this.limitWeight = limitWeight;
      |              ^~~~~~~~~~~
a.cc:59:14: error: request for member 'weight' in '(Solver*)this', which is of pointer type 'Solver*' (maybe you meant to use '->' ?)
   59 |         this.weight = weight;
      |              ^~~~~~
a.cc:59:23: error: 'weight' was not declared in this scope
   59 |         this.weight = weight;
      |                       ^~~~~~
a.cc:60:14: error: request for member 'value' in '(Solver*)this', which is of pointer type 'Solver*' (maybe you meant to use '->' ?)
   60 |         this.value = value;
      |              ^~~~~
a.cc:60:22: error: 'value' was not declared in this scope
   60 |         this.value = value;
      |                      ^~~~~
a.cc:61:9: error: 'answerArray' was not declared in this scope
   61 |         answerArray = new int[size][limitWeight + 1][2];
      |         ^~~~~~~~~~~
a.cc:61:55: error: array size in new-expression must be constant
   61 |         answerArray = new int[size][limitWeight + 1][2];
      |                                                       ^
a.cc:61:49: error: 'limitWeight' is not a constant expression
   61 |         answerArray = new int[size][limitWeight + 1][2];
      |                                     ~~~~~~~~~~~~^~~
a.cc: In member function 'int Solver::solve()':
a.cc:68:13: error: 'System' was not declared in this scope
   68 |             System.out.println("入力がありません。");
      |             ^~~~~~
a.cc:73:13: error: 'System' was not declared in this scope
   73 |             System.out.println("限界重量が不正な値です");
      |             ^~~~~~
a.cc:83:34: error: 'weight' was not declared in this scope
   83 |             int checkingWeight = weight[i];
      |                                  ^~~~~~
a.cc:84:33: error: 'value' was not declared in this scope
   84 |             int checkingValue = value[i];
      |                                 ^~~~~
a.cc:90:13: error: 'answerArray' was not declared in this scope
   90 |             answerArray[i][checkingWeight][0] = checkingWeight;
      |             ^~~~~~~~~~~
a.cc:132:34: error: 'ArrayIndexOutOfBoundsException' does not name a type
  132 |                         } catch (ArrayIndexOutOfBoundsException e)
      |                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:134:29: error: 'System' was not declared in this scope
  134 |                             System.out.println("Solver:anserArrayに対するアクセス違反");
      |                             ^~~~~~
a.cc:135:29: error: 'e' was not declared in this scope
  135 |                             e.printStackTrace();
      |                             ^