結果

問題 No.3409 How Many Gift Boxes?
コンテスト
ユーザー The Forsaking
提出日時 2025-12-23 22:06:07
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 877 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,139 ms
コンパイル使用メモリ 197,576 KB
実行使用メモリ 8,156 KB
最終ジャッジ日時 2025-12-23 22:06:13
合計ジャッジ時間 4,914 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:14:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   14 |     for (int i = 1; i < n + 1; i++) scanf("%d", a + i);
      |                                     ~~~~~^~~~~~~~~~~~~
main.cpp:15:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   15 |     for (int i = 1; i < m + 1; i++) scanf("%d", b + i);
      |                                     ~~~~~^~~~~~~~~~~~~

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
const int N = 2000010, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
int n, m, w[N];


int a[N], b[N];
int main() {
    cin >> n >> m;
    for (int i = 1; i < n + 1; i++) scanf("%d", a + i);
    for (int i = 1; i < m + 1; i++) scanf("%d", b + i);
    sort(a + 1, a + n + 1);
    sort(b + 1, b + m + 1);
    ll res = 0, l = 1;
    for (int i = 1; i < m + 1; i++) {
        while (l <= n && a[l] < b[i]) l++;
        a[l] == b[i] ? res -= a[l++] : 0;
        res += b[i];
    }
    for (int i = 1; i < n + 1; i++) res += a[i];
    printf("%lld\n", res % MOD);
    res = 0, l = 1;
    ll sum = 0;
    for (int i = 1; i < m + 1; i++) {
        while (l != n + 1 && a[l] <= b[i]) sum += a[l++];
        res = (res + sum + (n - l + 1) * b[i]) % MOD;
    }
    printf("%lld\n", res);
    return 0;
}
0