結果

問題 No.3201 Corporate Synergy
コンテスト
ユーザー Glemim
提出日時 2026-11-29 08:42:37
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc16.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
+ 398µs
コード長 18,560 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,806 ms
コンパイル使用メモリ 416,256 KB
実行使用メモリ 9,908 KB
最終ジャッジ日時 2026-12-01 23:43:05
合計ジャッジ時間 7,873 ms
ジャッジサーバーID
(参考情報)
judge6_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:264:30: warning: narrowing conversion of 'a' from 'std::tuple_element<0, std::tuple<long long int, long long int, long long int> >::type' {aka 'long long int'} to 'int' [-Wnarrowing]
  264 |     bb.set_gain_to_burn_all({a,b},s);
      |                              ^
main.cpp:264:30: warning: narrowing conversion of 'a' from 'std::tuple_element<0, std::tuple<long long int, long long int, long long int> >::type' {aka 'long long int'} to 'int' [-Wnarrowing]
main.cpp:264:32: warning: narrowing conversion of 'b' from 'std::tuple_element<1, std::tuple<long long int, long long int, long long int> >::type' {aka 'long long int'} to 'int' [-Wnarrowing]
  264 |     bb.set_gain_to_burn_all({a,b},s);
      |                                ^
main.cpp:264:32: warning: narrowing conversion of 'b' from 'std::tuple_element<1, std::tuple<long long int, long long int, long long int> >::type' {aka 'long long int'} to 'int' [-Wnarrowing]

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
constexpr long long INF=2002002002002002002LL;
constexpr double PI=3.14159265358979323846;
constexpr long long dx[]={ 1, 0, -1,  0, 1, -1, -1,  1, 0};
constexpr long long dy[]={ 0, 1,  0, -1, 1,  1, -1, -1, 0};
constexpr string yesno[]={"No\n","Yes\n"};
constexpr char DRUL[]="DRUL";
constexpr unsigned long long TEN[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000,1000000000000,10000000000000,100000000000000,1000000000000000,10000000000000000,100000000000000000,1000000000000000000,10000000000000000000ULL};
using ll=long long;
using ull=unsigned long long;
using mint=atcoder::modint998244353;
template<class T> using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class U,class V> pair<U,V> operator+(const pair<U,V> &lhs, const pair<U,V> &rhs) { return {lhs.first+rhs.first,lhs.second+rhs.second}; }
template<class U,class V> pair<U,V> operator-(const pair<U,V> &lhs, const pair<U,V> &rhs) { return {lhs.first-rhs.first,lhs.second-rhs.second}; }
template<class U,class V> pair<U,V> &operator+=(pair<U,V> &lhs, const pair<U,V> &rhs) { return lhs=lhs+rhs; }
template<class U,class V> pair<U,V> &operator-=(pair<U,V> &lhs, const pair<U,V> &rhs) { return lhs=lhs-rhs; }
template<class T> pair<T,T> operator*(const pair<T,T> &lhs, const T &rhs) { return {lhs.first*rhs,lhs.second*rhs}; }
template<class T> pair<T,T> operator*(const T &lhs, const pair<T,T> &rhs) { return {lhs*rhs.first,lhs*rhs.second}; }
template<class T> pair<T,T> operator/(const pair<T,T> &lhs, const T &rhs) { return {lhs.first/rhs,lhs.second/rhs}; }
template<class T> T operator*(const pair<T,T> &lhs, const pair<T,T> &rhs) { return lhs.first*rhs.second-rhs.first*lhs.second; }
template<class T> inline T hypotsq(const pair<T,T> &p) { return p.first*p.first+p.second*p.second; }
template<class T> inline double hypot(const pair<T,T> &p) { return hypot<double>(p.first,p.second); }
template<class T> inline T hypotsq(T x, T y) { return x*x+y*y; }
template<class T> inline T hypotsq(T px, T py, T qx, T qy) { return hypotsq(qx-px,qy-py); }
template<class T> inline double hypot(T px, T py, T qx, T qy) { return hypot<double>(qx-px,qy-py); }
template<int M> istream &operator>>(istream &is, atcoder::static_modint<M> &x) { long long _x; is>>_x; x=_x; return is; }
template<int M> ostream &operator<<(ostream &os, const atcoder::static_modint<M> &x) { return os<<x.val(); }
template<int M> istream &operator>>(istream &is, atcoder::dynamic_modint<M> &x) { long long _x; is>>_x; x=_x; return is; }
template<int M> ostream &operator<<(ostream &os, const atcoder::dynamic_modint<M> &x) { return os<<x.val(); }
istream &operator>>(istream &is, __int128_t &x) { string S; is>>S; x=0; bool neg=false; for (auto &c : S) { if (c=='-') neg=true; else x=x*10+c-'0'; } if (neg) x=-x; return is; }
ostream &operator<<(ostream &os, __int128_t x) { if (x==0) return os<<0; if (x<0) os<<'-', x=-x; string S; while (x) S.push_back('0'+x%10), x/=10; reverse(begin(S),end(S)); return os<<S; }
istream &operator>>(istream &is, __uint128_t &x) { string S; is>>S; x=0; for (auto &c : S) x=x*10+c-'0'; return is; }
ostream &operator<<(ostream &os, __uint128_t x) { if (x==0) return os<<0; string S; while (x) S.push_back('0'+x%10), x/=10; reverse(begin(S), end(S)); return os<<S; }
template<class U,class V> istream &operator>>(istream &is, pair<U,V> &p);
template<class U,class V> ostream &operator<<(ostream &os, const pair<U,V> &p);
template<class ...T> istream &operator>>(istream &is, tuple<T...> &t);
template<class ...T> ostream &operator<<(ostream &os, const tuple<T...> &t);
template<class T> istream &operator>>(istream &is, vector<T> &v);
template<class T> ostream &operator<<(ostream &os, const vector<T> &v);
template<class T> ostream &operator<<(ostream &os, const optional<T> &x);
template<class U,class V> istream &operator>>(istream &is, pair<U,V> &p) { return is>>p.first>>p.second; }
template<class U,class V> ostream &operator<<(ostream &os, const pair<U,V> &p) { return os<<p.first<<' '<<p.second; }
template<class ...T> istream &operator>>(istream &is, tuple<T...> &t) { apply([&](auto &...args) { ((is>>args), ...); },t); return is; }
template<class ...T> ostream &operator<<(ostream &os, const tuple<T...> &t) { apply([&](auto &...args) { int n=0; ((os<<(n++?" ":"")<<args), ...); },t); return os;}
template<class T> istream &operator>>(istream &is, vector<T> &v) { for (auto &x : v) is>>x; return is; }
template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i=0,s=(int)v.size();i<s;++i) os<<(i?" ":"")<<v[i]; return os; }
template<class T> ostream &operator<<(ostream &os, const set<T> &v) { auto it=v.begin(); while (it!=v.end()) os<<*it++<<(it==v.end()?"":" "); return os; }
template<class T> ostream &operator<<(ostream &os, const unordered_set<T> &v) { auto it=v.begin(); while (it!=v.end()) os<<*it++<<(it==v.end()?"":" "); return os; }
template<class T> ostream &operator<<(ostream &os, const multiset<T> &v) { auto it=v.begin(); while (it!=v.end()) os<<*it++<<(it==v.end()?"":" "); return os; }
template<class T> ostream &operator<<(ostream &os, const list<T> &v) { auto it=v.begin(); while (it!=v.end()) os<<*it++<<(it==v.end()?"":" "); return os; }
template<class T> ostream &operator<<(ostream &os, const stack<T> &v) { stack<T> u=v; while (!u.empty()) { os<<u.top(); u.pop(); if (!u.empty()) os<<' '; } return os; }
template<class T> ostream &operator<<(ostream &os, const queue<T> &v) { queue<T> u=v; while (!u.empty()) { os<<u.front(); u.pop(); if (!u.empty()) os<<' '; } return os; }
template<class T> ostream &operator<<(ostream &os, const deque<T> &v) { deque<T> u=v; while (!u.empty()) { os<<u.front(); u.pop_front(); if (!u.empty()) os<<' '; } return os; }
template<class T,class U,class V> ostream &operator<<(ostream &os, const priority_queue<T,U,V> &v) { priority_queue<T,U,V> u=v; while (!u.empty()) { os<<u.top(); u.pop(); if (!u.empty()) os<<' '; } return os; }
template<class T,class U> ostream &operator<<(ostream &os, const map<T,U> &m) { auto it=m.begin(); while (it!=m.end()) os<<(it->first)<<':'<<(it++->second)<<(it==m.end()?"":" "); return os; }
template<class T,class U> ostream &operator<<(ostream &os, const unordered_map<T,U> &m) { auto it=m.begin(); while (it!=m.end()) os<<(it->first)<<':'<<(it++->second)<<(it==m.end()?"":" "); return os; }
template<class T> ostream &operator<<(ostream &os, const optional<T> &x) {  if (x.has_value()) return os<<x.value(); return os<<"-1"; }
template<class T,class U> inline T floor(const T &a, const U &b) { return a/b-(a%b && (a^b)< 0); }
template<class T,class U> inline T ceil (const T &a, const U &b) { return a/b+(a%b && (a^b)>=0); }
inline double radian(double deg) { return (PI/180.0)*deg; }
inline double degree(double rad) { return (180.0/PI)*rad; }
inline long long popcount(unsigned long long x) { return __builtin_popcountll(x); }
inline long long height(unsigned long long x) { if (x==0) return 0LL; return 64LL-__builtin_clzll(x); }
inline long long ctz(unsigned long long x) { return __builtin_ctzll(x); }
inline long long lsb(long long b) { return b&-b; }
template<class T> long long sgn(const T &x) { if (x<0) return -1; return x>0; }
template<class T> inline typename enable_if<is_integral_v<T>,T>::type rem(T a, T b) { assert(b>0); T res=a%b; if (res<0) res+=b; return res; }
template<class T> inline typename enable_if<is_floating_point_v<T>,T>::type rem(T a, T b) { assert(b>0); T res=a-floor(a/b)*b; if (res<0.0) res+=b; return res; }
template<class T> inline bool inlr(const T &l, const T &x, const T &r) noexcept { return (l<=x && x<r); }
template<class T> bool isinc(T first, T second) { return first<=second; }
template<class T,class ...Rest> bool isinc(T first, T second, const Rest ...rest) { return first<=second && isinc(second,rest...); }
long long safe_mul() noexcept { return 1LL; }
template<class ...Rest> long long safe_mul(long long a, Rest ...rest) noexcept { if (a==0LL) return 0LL; long long b=safe_mul(rest...); if (b==0LL) return 0LL; if (INF/a/b==0LL) return ((0LL<a)^(0LL<b) ? -INF : INF); return a*b; }
template<class T> void println(T item) { cout<<item<<'\n'; }
template<class T,class ...Rest> void println(T item, Rest ...rest) { cout<<item<<' '; println(rest...); }
template<class T> void dump(T item) { cerr<<item<<'\n'; }
template<class T,class ...Rest> void dump(T item, Rest ...rest) { cerr<<item<<' '; dump(rest...); }
long long safe_lcm(long long a, long long b) { if (a==0 || b==0) return 0LL; if (a<0) a=-a; if (b<0) b=-b; if (a>=INF || b>=INF) return INF; return safe_mul(a/gcd(a,b),b); }
template<class Res=long long> vector<Res> mkiota(int n, Res val=0) { vector<Res> res(n); iota(begin(res),end(res),val); return res; }
template<class T> T mkrev(const T &v) { T res=v; reverse(res.begin(),res.end()); return res; }
template<class T> vector<T> mksum(const vector<T> &v) { int s=v.size(); vector<T> res(s+1); for (int i=0;i<s;++i) res[i+1]=res[i]+v[i]; return res; }
template<class T> vector<T> mkdif(const vector<T> &v) { int s=v.size(); vector<T> res(s-1); for (int i=0;i+1<s;++i) res[i]=v[i+1]-v[i]; return res; }
template<class T> vector<T> mkuni(const vector<T> &v) { auto res=v; sort(res.begin(),res.end()); res.erase(unique(res.begin(),res.end()),res.end()); return res; }
template<class Res=long long,class T> vector<Res> mkinv(const vector<T> &v) { vector<Res> res(*max_element(v.begin(),v.end())+1,-1); for (int i=0,s=v.size();i<s;++i) res[v[i]]=i; return res; }
template<class Res=long long,class F> vector<Res> mkord(int n, F f) { vector<Res> res(n); iota(res.begin(),res.end(),0); stable_sort(res.begin(),res.end(),f); return res; }
template<class T> map<T,long long> mkcnt(const vector<T> &v) { map<T,long long> res; for (const auto &x : v) res[x]++; return res; }
template<class T,class F> vector<vector<T>> mksplit(const vector<T> &v, F f) { if (v.size()==0) return {}; vector<vector<T>> res{{v[0]}}; for (int i=0,s=v.size();i+1<s;++i) { if (f(v[i],v[i+1])) res.push_back({}); res.back().push_back(v[i+1]); } return res; }
template<class T> vector<pair<T,long long>> mkrle(const vector<T> &v) { int n=v.size(); vector<pair<T,long long>> res; for (int i=0;i<n;++i) { long long cnt=1; while (i+1<n && v[i]==v[i+1]) { cnt++; i++; } res.emplace_back(v[i],cnt); } return res; }
vector<pair<char,long long>> mkrle(const string &s) { int n=s.size(); vector<pair<char,long long>> res; for (int i=0;i<n;++i) { long long cnt=1; while (i+1<n && s[i]==s[i+1]) { cnt++; i++; } res.emplace_back(s[i],cnt); } return res; }
template<class T> atcoder::fenwick_tree<T> mkfen(const vector<T> &v) { int s=v.size(); atcoder::fenwick_tree<T> res(s); for (int i=0;i<s;++i)  res.add(i,v[i]); return res; }
template<class T> T min(const vector<T> &v) { return *min_element(v.begin(),v.end()); }
template<class T> T max(const vector<T> &v) { return *max_element(v.begin(),v.end()); }
template<class T> inline void sort(T &v, bool rev=false) { rev ? sort(v.rbegin(),v.rend()) : sort(v.begin(),v.end()); }
template<class T,class U> long long count(const T &v, const U &item) { return count(v.begin(),v.end(),item); }
template<class T> inline long long count_in_range(const vector<T> &v, const T &l, const T &r) { return lower_bound(v.begin(),v.end(),r)-lower_bound(v.begin(),v.end(),l); }
template<class T> inline long long lob(const vector<T> &v, const T &x) { return lower_bound(v.begin(),v.end(),x)-v.begin(); }
template<class T> inline long long upb(const vector<T> &v, const T &x) { return upper_bound(v.begin(),v.end(),x)-v.begin(); }
template<class T> optional<long long> minel(const vector<T> &v, size_t k=0) { if (v.size()<=k) return nullopt; return mkord(v.size(),[&](long long a, long long b)->bool { return v[a]<v[b]; })[k]; }
template<class T> optional<long long> maxel(const vector<T> &v, size_t k=0) { if (v.size()<=k) return nullopt; return mkord(v.size(),[&](long long a, long long b)->bool { return v[a]>v[b]; })[k]; }
template<class T> T flatten(const vector<T> &vv) { T res; for (const T &v : vv) res.insert(res.end(),v.begin(),v.end()); return res; }
vector<long long> binary_split(long long x) { assert(x>=0); vector<long long> res; long long k=1; while (x>0) { long long y=min(k,x); res.push_back(y); x-=y; k<<=1; } return res; }
template<class T> T arithmetic_series(T a, T d, T n) { return (a*2+(n-1)*d)*n/2; }
template<class T> long long hamming_distance(const T &a, const T &b) { assert(a.size()==b.size()); auto ita=a.begin(); auto itb=b.begin(); int res=0; while (ita!=a.end()) if (*ita++!=*itb++) res++; return res; }
template<class T> long long get_period(const T &v) { int n=v.size(); vector<int> z=atcoder::z_algorithm(v); for (int i=1;i<=n/2;++i) if (n%i==0 && z[i]==n-i) return i; return n; }
template<class T> void cumulative_min(vector<T> &v, bool ltr) { int n=v.size(); if (ltr) for (int i=0;i+1<n;++i) v[i+1]=min(v[i+1],v[i]); else for (int i=n-2;i>=0;--i) v[i]=min(v[i],v[i+1]); }
template<class T> void cumulative_max(vector<T> &v, bool ltr) { int n=v.size(); if (ltr) for (int i=0;i+1<n;++i) v[i+1]=max(v[i+1],v[i]); else for (int i=n-2;i>=0;--i) v[i]=max(v[i],v[i+1]); }
template<class T> void repeat_vector(vector<T> &v, int count=2) { assert(count>=0); if (count==0) { v.clear(); return; } count--; int original_size=v.size(); while (count--) v.insert(v.end(),v.begin(),v.begin()+original_size); }
template<class T> long long longest_continuum(const vector<T> &v) { if (v.empty()) return 0; long long res=1; long long now=1; int n=v.size(); for (int i=1;i<n;++i) if (v[i]==v[i-1]) chmax(res,++now); else now=1; return res; }
template<class T> long long longest_continuum(const vector<T> &v, const T &item) { long long res=0; long long now=0; for (auto &x : v) if (x==item) chmax(res,++now); else now=0; return res; }
template<class T> long long lis_len(const vector<T> &v, bool strict=true) { vector<T> dp; for (auto &x : v) { auto it=strict ? lower_bound(dp.begin(),dp.end(),x) : upper_bound(dp.begin(),dp.end(),x); if (it==dp.end()) dp.push_back(x); else *it=x; } return dp.size(); }
template<class T> long long inversions(vector<T> &v) { map<T,int> cc; for (auto &x : v) cc[x]=0; int M=0; for (auto &[key,val] : cc) val=M++; long long res=0; atcoder::fenwick_tree<long long> fen(M); for (auto &x : v) { int y=cc[x]; res+=fen.sum(y+1,M); fen.add(y,1); } return res; }
long long ctok(char d) { switch (d) { case 'D': return 0; case 'R': return 1; case 'U': return 2; case 'L': return 3; } return -1; }
template<class T> T min_op(T a, T b) { return min(a,b); }
template<class T> T max_op(T a, T b) { return max(a,b); }
template<class T> using min_segtree=atcoder::segtree<T,min_op<T>,numeric_limits<T>::max>;
template<class T> using max_segtree=atcoder::segtree<T,max_op<T>,numeric_limits<T>::lowest>;

template<class T=long long, T INF=std::numeric_limits<T>::max()/4> class BurnBury {
private:
  int n;
  std::vector<T> cost0;
  std::vector<T> cost1;
  std::unordered_map<long long,T> cost01;

  struct SuperNode {
    T weight;
    std::vector<int> to;
  };
  std::vector<SuperNode> cost0sn;
  std::vector<SuperNode> cost1sn;
  std::vector<SuperNode> gain0sn;
  std::vector<SuperNode> gain1sn;
  bool solved=false;
  std::vector<bool> min_cut;
public:
  BurnBury(int n) : n(n), cost0(n,0), cost1(n,0) {
  }
  void set_cost_to_burn(int i, T cost) {
    assert(!solved);
    assert(0<=i && i<n);
    cost0[i]=cost;
  }
  void set_cost_to_bury(int i, T cost) {
    assert(!solved);
    assert(0<=i && i<n);
    cost1[i]=cost;
  }
  void set_cost_to_burn_i_and_bury_j(int i, int j, T cost) {
    assert(!solved);
    assert(0<=i && i<n);
    assert(0<=j && j<n);
    assert(cost>=0);
    cost01[(long long)i*n+j]=cost;
  }
  void set_cost_to_burn_some(std::vector<int> is, T cost) {
    assert(!solved);
    assert(cost>=0);
    if (cost>0) cost0sn.emplace_back(cost,is);
  }
  void set_cost_to_bury_some(std::vector<int> is, T cost) {
    assert(!solved);
    assert(cost>=0);
    if (cost>0) cost1sn.emplace_back(cost,is);
  }
  void set_gain_to_burn_all(std::vector<int> is, T gain) {
    assert(!solved);
    assert(gain>=0);
    if (gain>0) gain0sn.emplace_back(gain,is);
  }
  void set_gain_to_bury_all(std::vector<int> is, T gain) {
    assert(!solved);
    assert(gain>=0);
    if (gain>0) gain1sn.emplace_back(gain,is);
  }
  T solve() {
    atcoder::mf_graph<T> mfg;
    T total_gain=0;
    int s=n+cost0sn.size()+cost1sn.size()+gain0sn.size()+gain1sn.size();
    int t=s+1;
    mfg=atcoder::mf_graph<T>(t+1);
    for (int i=0;i<n;++i) {
      T cost_sum0=0;
      T cost_sum1=0;
      if (cost0[i]>=0) {
        cost_sum0+=cost0[i];
      } else {
        total_gain-=cost0[i];
        cost_sum1-=cost0[i];
      }
      if (cost1[i]>=0) {
        cost_sum1+=cost1[i];
      } else {
        total_gain-=cost1[i];
        cost_sum0-=cost1[i];
      }
      if (cost_sum0!=0) mfg.add_edge(i,t,cost_sum0);
      if (cost_sum1!=0) mfg.add_edge(s,i,cost_sum1);
    }
    for (auto [key,cost] : cost01) {
      if (cost==0) continue;
      int i=key/n;
      int j=key%n;
      mfg.add_edge(i,j,cost);
    }
    int k=n;
    for (auto x : cost0sn) {
      for (auto v : x.to) mfg.add_edge(v,k,INF);
      mfg.add_edge(k++,t,x.weight);
    }
    for (auto x : cost1sn) {
      for (auto v : x.to) mfg.add_edge(k,v,INF);
      mfg.add_edge(s,k++,x.weight);
    }
    for (auto x : gain0sn) {
      total_gain+=x.weight;
      for (auto v : x.to) mfg.add_edge(k,v,INF);
      mfg.add_edge(s,k++,x.weight);
    }
    for (auto x : gain1sn) {
      total_gain+=x.weight;
      for (auto v : x.to) mfg.add_edge(v,k,INF);
      mfg.add_edge(k++,t,x.weight);
    }
    T ans=mfg.flow(s,t,INF);
    if (ans<INF) ans-=total_gain;
    min_cut=mfg.min_cut(s);
    solved=true;
    return ans;
  }
  bool was_burned(int i) {
    assert(solved);
    return min_cut[i];
  }
};

int main() {
  //yukicoder No.3201
  ll N;
  cin>>N;
  vector<ll> P(N);
  cin>>P;
  ll M;
  cin>>M;
  vector<pair<ll,ll>> UV(M);
  cin>>UV;
  ll K;
  cin>>K;
  vector<tuple<ll,ll,ll>> ABS(K);
  cin>>ABS;

  BurnBury bb(N);
  for (ll i=0;i<N;++i) bb.set_cost_to_burn(i,-P[i]);
  for (auto [u,v] : UV) {
    u--,v--;
    bb.set_cost_to_burn_i_and_bury_j(v,u,INF);
  }
  for (ll i=0;i<K;++i) {
    auto [a,b,s]=ABS[i];
    a--,b--;
    bb.set_gain_to_burn_all({a,b},s);
  }
  cout<<-bb.solve()<<endl;

}
0