< kiiroimomoさんのAI生成コード置き場

bから0個以上選んでa+bを更新しながら最大値を求める

投稿者kiiroimomo
言語C++23
更新日時 (JST)2026-10-17 07:38:34
サイズ664 Byte
参照URL

日時はサーバーが記録したものです。更新すると更新日時が変わるので、不都合がある場合は新しく投稿してください。

テキストで開く
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int INF=1e9;

 /* Library:
  https://yukicoder.me/users/24530/ai_code */

int main() {
	cin.tie(nullptr),cout.tie(nullptr);
	ios::sync_with_stdio(false);
  
  int n,m;
  cin >> n >> m;
  
  vector<int> a(n);
  for(int i=0; i<n; i++){
    cin >> a[i];
  }
  sort(a.rbegin(),a.rend());
  
  vector<int> b(m);
  for(int i=0; i<m; i++){
    cin >> b[i];
  }
  sort(b.rbegin(),b.rend());

  ll kai=0;
  ll p=0;
  ll k=0;
  ll maxw=0;
    for(int i=0; i<n; i++){
     p+=a[i];
     if(i<m){
       k+=b[i];
       maxw=max(maxw,k);
     }
     kai=max(kai,p+maxw);
    }

    cout << kai << endl;
}