結果
| 問題 | No.15 カタログショッピング | 
| コンテスト | |
| ユーザー |  バカらっく | 
| 提出日時 | 2019-09-01 05:55:26 | 
| 言語 | Kotlin (2.1.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,825 ms / 5,000 ms | 
| コード長 | 2,073 bytes | 
| コンパイル時間 | 21,865 ms | 
| コンパイル使用メモリ | 452,460 KB | 
| 実行使用メモリ | 142,616 KB | 
| 最終ジャッジ日時 | 2024-11-27 06:23:50 | 
| 合計ジャッジ時間 | 25,593 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 10 | 
コンパイルメッセージ
Main.kt:3:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^
            
            ソースコード
import java.lang.StringBuilder
fun main(args: Array<String>) {
    val(n,s) = readLine()!!.trim().split(" ").map { it.toInt() }
    total = s
    for(i in 1..n) {
        itemList.add(Item(i-1, readLine()!!.toInt()))
    }
    itemList.sortByDescending { it.value }
    val zenhan = getHalfList(itemList.subList(0, itemList.size / 2), 0, 0)
    val kouhan = getHalfList(itemList.subList(itemList.size / 2, itemList.size), 0, 0)
    val ans = mutableListOf<Int>()
    zenhan.filter { it.key <= total }.forEach{
        val key = total - it.key
        val zen = it.value
        kouhan[key]?.let {
            it.forEach {
                val kou = it
                zen.forEach {
                    ans.add(it + kou)
                }
            }
        }
    }
    ans.sortDescending()
    ans.forEach { println(convAns(it)) }
}
fun convAns(num:Int):String {
    val ans = StringBuilder()
    for(i in itemList.indices) {
        val key = 1 shl (itemList.lastIndex - i)
        if(num and key > 0) {
            ans.append(" ")
            ans.append(i + 1)
        }
    }
    return ans.toString().substring(1)
}
var total = 0
class Item(val index:Int, val value:Int)
val itemList = mutableListOf<Item>()
fun getHalfList(list:List<Item>, index:Int, subTotal:Int):Map<Int, List<Int>> {
    if(index > list.lastIndex) {
        val ret = mutableMapOf<Int, List<Int>>()
        ret[subTotal] = listOf(0)
        return ret
    }
    val ans = mutableMapOf<Int, MutableList<Int>>()
    val ret1 = getHalfList(list, index + 1, subTotal + list[index].value)
    val ret2 = getHalfList(list, index + 1, subTotal)
    val key = 1 shl (itemList.lastIndex - list[index].index)
    ret1.forEach{
        val total = it.key
        if(!ans.containsKey(total)) {
            ans[total] = mutableListOf()
        }
        ans[total]!!.addAll(it.value.map { it + key })
    }
    ret2.forEach {
        val total = it.key
        if(!ans.containsKey(total)) {
            ans[total] = mutableListOf()
        }
        ans[total]!!.addAll(it.value)
    }
    return ans
}
            
            
            
        