結果
| 問題 | 
                            No.1148 土偶Ⅲ
                             | 
                    
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-08-05 19:56:10 | 
| 言語 | Java  (openjdk 23)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 228 ms / 2,000 ms | 
| コード長 | 1,024 bytes | 
| コンパイル時間 | 2,182 ms | 
| コンパイル使用メモリ | 77,888 KB | 
| 実行使用メモリ | 57,808 KB | 
| 最終ジャッジ日時 | 2024-09-17 10:10:50 | 
| 合計ジャッジ時間 | 7,193 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge6 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 25 | 
ソースコード
import java.io.*;
import java.util.HashSet;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        int n = Integer.parseInt(line[0]);
        int w = Integer.parseInt(line[1]);
        int a[] = new int[n];
        for(int i = 0; i < n; i++)  a[i] = Integer.parseInt(br.readLine());
        int l = 0, r = 0, sum = 0, ans = 0;
        HashSet<Integer> s = new HashSet<Integer>();
        while(r < n){
            if(sum+a[r] > w){
                sum -= a[l];
                s.remove(a[l++]);
                continue;
            }
            if(s.contains(a[r])){
                while(s.contains(a[r])){
                    sum -= a[l];
                    s.remove(a[l++]);
                }
            }
            sum += a[r];
            s.add(a[r++]);
            ans = Math.max(ans, r-l);
        }
        System.out.println(ans);
    }
}