結果

問題 No.617 Nafmo、買い出しに行く
コンテスト
ユーザー bal4u
提出日時 2019-05-19 21:10:38
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 1,450 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 235 ms
コンパイル使用メモリ 40,136 KB
最終ジャッジ日時 2026-02-22 03:27:25
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// yukicoder: 617 Nafmo、買い出しに行く
// 2019.5.19 bal4u

#include <stdio.h>
#include <stdlib.h>

#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

int A[22], w;
int a[362900], aw;

// 見つからなければ、一つ小さい要素を返す
int bsch(int x)
{
	int m, l = 0, r = aw;

    while (l < r) {
        m = (l+r) >> 1;
        if (a[m] <= x) l = m + 1; else r = m;
    }
	if (l == 0) return 0;
	return a[l-1];
}

int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; }

int main()
{
	int i, j, N, K, s, sz, lim, ans;
	
	N = in(), K = in();
	if (K == 0) { puts("0"); return 0; }
	
	s = 0, w = 0; while (N--) {
		A[w] = in();
		if (A[w] <= K) { s += A[w]; w++; }
	}
	if (s <= K) { printf("%d\n", s); return 0; }

	sz = w >> 1; if (sz > 9) sz = 9;
	N = sz, lim = 1 << sz, aw = 0;
	for (i = 1; i < lim; i++) {
		s = 0;
		for (j = 0; j < sz; j++) if ((i >> j) & 1) s += A[j];
		if (s == K) goto done;
		if (s < K) a[aw++] = s;
	}
	qsort(a, aw, sizeof(int), cmp);
	
	ans = a[aw-1], sz = w - N, lim = 1 << sz;
	for (i = 1; i < lim; i++) {
		s = 0;
		for (j = 0; j < sz; j++) if ((i >> j) & 1) s += A[j+N];
		if (s == K) goto done;
		if (s < K) {
			s += bsch(K-s);
			if (s > ans) ans = s;
		}
	}
	printf("%d\n", ans);
	return 0;
done: printf("%d\n", K);
	return 0;
}
0