This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://atcoder.jp/contests/abc270/tasks/abc270_f
// competitive-verifier: IGNORE
#include <iostream>
#include <vector>
#include <limits>
#include "tools/prim.hpp"
#include "tools/popcount.hpp"
#include "tools/chmin.hpp"
using ll = long long;
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
ll N, M;
std::cin >> N >> M;
std::vector<ll> X(N), Y(N);
for (auto& X_i : X) std::cin >> X_i;
for (auto& Y_i : Y) std::cin >> Y_i;
std::vector<ll> A(M), B(M), Z(M);
for (ll i = 0; i < M; ++i) {
std::cin >> A[i] >> B[i] >> Z[i];
--A[i], --B[i];
}
ll answer = std::numeric_limits<ll>::max();
for (ll state = 0; state < 4; ++state) {
tools::prim<ll> graph(N + tools::popcount(state));
if (state & 1) {
for (ll i = 0; i < N; ++i) {
graph.add_edge(i, N, X[i]);
}
}
if (state & 2) {
for (ll i = 0; i < N; ++i) {
graph.add_edge(i, N + (state & 1), Y[i]);
}
}
for (ll i = 0; i < M; ++i) {
graph.add_edge(A[i], B[i], Z[i]);
}
if (const auto groups = graph.query().first; groups.size() == 1) {
tools::chmin(answer, groups[0].first);
}
}
std::cout << answer << '\n';
return 0;
}
#line 1 "tests/prim/unconnected.test.cpp"
// competitive-verifier: PROBLEM https://atcoder.jp/contests/abc270/tasks/abc270_f
// competitive-verifier: IGNORE
#include <iostream>
#include <vector>
#include <limits>
#line 1 "tools/prim.hpp"
#include <algorithm>
#include <cassert>
#include <cstddef>
#line 8 "tools/prim.hpp"
#include <queue>
#include <tuple>
#include <utility>
#line 1 "tools/greater_by.hpp"
namespace tools {
template <class F>
class greater_by {
private:
F selector;
public:
greater_by(const F& selector) : selector(selector) {
}
template <class T>
bool operator()(const T& x, const T& y) const {
return selector(x) > selector(y);
}
};
}
#line 13 "tools/prim.hpp"
namespace tools {
template <typename T>
class prim {
public:
struct edge {
::std::size_t id;
::std::size_t from;
::std::size_t to;
T cost;
};
private:
::std::vector<edge> m_edges;
::std::vector<::std::vector<::std::size_t>> m_graph;
public:
prim() = default;
prim(const ::tools::prim<T>&) = default;
prim(::tools::prim<T>&&) = default;
~prim() = default;
::tools::prim<T>& operator=(const ::tools::prim<T>&) = default;
::tools::prim<T>& operator=(::tools::prim<T>&&) = default;
explicit prim(const ::std::size_t n) : m_graph(n) {
}
::std::size_t size() const {
return this->m_graph.size();
}
::std::size_t add_edge(::std::size_t u, ::std::size_t v, const T w) {
assert(u < this->size());
assert(v < this->size());
::std::tie(u, v) = ::std::minmax({u, v});
this->m_edges.push_back(edge({this->m_edges.size(), u, v, w}));
this->m_graph[u].push_back(this->m_edges.size() - 1);
this->m_graph[v].push_back(this->m_edges.size() - 1);
return this->m_edges.size() - 1;
}
const edge& get_edge(const ::std::size_t k) const {
assert(k < this->m_edges.size());
return this->m_edges[k];
}
const ::std::vector<edge>& edges() const {
return this->m_edges;
}
::std::pair<::std::vector<::std::pair<T, ::std::vector<::std::size_t>>>, ::std::vector<::std::size_t>> query() const {
::std::pair<::std::vector<::std::pair<T, ::std::vector<::std::size_t>>>, ::std::vector<::std::size_t>> res;
auto& [groups, belongs_to] = res;
belongs_to.resize(this->size());
::std::fill(belongs_to.begin(), belongs_to.end(), ::std::numeric_limits<::std::size_t>::max());
for (::std::size_t root = 0; root < this->size(); ++root) {
if (belongs_to[root] < ::std::numeric_limits<::std::size_t>::max()) continue;
const auto greater_by_cost = ::tools::greater_by([&](const auto& pair) { return this->m_edges[pair.first].cost; });
::std::priority_queue<::std::pair<::std::size_t, ::std::size_t>, ::std::vector<::std::pair<::std::size_t, ::std::size_t>>, decltype(greater_by_cost)> pq(greater_by_cost);
groups.emplace_back(0, ::std::vector<::std::size_t>());
auto& [total_cost, edge_ids] = groups.back();
belongs_to[root] = groups.size() - 1;
for (const auto e : this->m_graph[root]) {
const auto next = this->m_edges[e].from ^ this->m_edges[e].to ^ root;
if (belongs_to[next] < ::std::numeric_limits<::std::size_t>::max()) continue;
pq.emplace(e, next);
}
while (!pq.empty()) {
const auto [from_edge_id, here] = pq.top();
pq.pop();
if (belongs_to[here] < ::std::numeric_limits<::std::size_t>::max()) continue;
belongs_to[here] = belongs_to[root];
total_cost += this->m_edges[from_edge_id].cost;
edge_ids.push_back(from_edge_id);
for (const auto e : this->m_graph[here]) {
const auto next = this->m_edges[e].from ^ this->m_edges[e].to ^ here;
if (belongs_to[next] < ::std::numeric_limits<::std::size_t>::max()) continue;
pq.emplace(e, next);
}
}
}
return res;
}
};
}
#line 1 "tools/popcount.hpp"
#include <bit>
#line 6 "tools/popcount.hpp"
#include <type_traits>
#line 1 "tools/is_integral.hpp"
#line 5 "tools/is_integral.hpp"
namespace tools {
template <typename T>
struct is_integral : ::std::is_integral<T> {};
template <typename T>
inline constexpr bool is_integral_v = ::tools::is_integral<T>::value;
}
#line 1 "tools/is_signed.hpp"
#line 5 "tools/is_signed.hpp"
namespace tools {
template <typename T>
struct is_signed : ::std::is_signed<T> {};
template <typename T>
inline constexpr bool is_signed_v = ::tools::is_signed<T>::value;
}
#line 1 "tools/make_unsigned.hpp"
#line 5 "tools/make_unsigned.hpp"
namespace tools {
template <typename T>
struct make_unsigned : ::std::make_unsigned<T> {};
template <typename T>
using make_unsigned_t = typename ::tools::make_unsigned<T>::type;
}
#line 1 "tools/uint128_t.hpp"
#line 1 "tools/detail/int128_t.hpp"
#line 7 "tools/detail/int128_t.hpp"
#include <cstdint>
#include <functional>
#line 11 "tools/detail/int128_t.hpp"
#include <string>
#include <string_view>
#line 1 "tools/abs.hpp"
namespace tools {
constexpr float abs(const float x) {
return x < 0 ? -x : x;
}
constexpr double abs(const double x) {
return x < 0 ? -x : x;
}
constexpr long double abs(const long double x) {
return x < 0 ? -x : x;
}
constexpr int abs(const int x) {
return x < 0 ? -x : x;
}
constexpr long abs(const long x) {
return x < 0 ? -x : x;
}
constexpr long long abs(const long long x) {
return x < 0 ? -x : x;
}
constexpr unsigned int abs(const unsigned int x) {
return x;
}
constexpr unsigned long abs(const unsigned long x) {
return x;
}
constexpr unsigned long long abs(const unsigned long long x) {
return x;
}
}
#line 1 "tools/bit_ceil.hpp"
#line 10 "tools/bit_ceil.hpp"
namespace tools {
template <typename T>
constexpr T bit_ceil(T) noexcept;
template <typename T>
constexpr T bit_ceil(const T x) noexcept {
static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
if constexpr (::tools::is_signed_v<T>) {
assert(x >= 0);
return ::tools::bit_ceil<::tools::make_unsigned_t<T>>(x);
} else {
return ::std::bit_ceil(x);
}
}
}
#line 1 "tools/bit_floor.hpp"
#line 10 "tools/bit_floor.hpp"
namespace tools {
template <typename T>
constexpr T bit_floor(T) noexcept;
template <typename T>
constexpr T bit_floor(const T x) noexcept {
static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
if constexpr (::tools::is_signed_v<T>) {
assert(x >= 0);
return ::tools::bit_floor<::tools::make_unsigned_t<T>>(x);
} else {
return ::std::bit_floor(x);
}
}
}
#line 1 "tools/bit_width.hpp"
#line 10 "tools/bit_width.hpp"
namespace tools {
template <typename T>
constexpr int bit_width(T) noexcept;
template <typename T>
constexpr int bit_width(const T x) noexcept {
static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
if constexpr (::tools::is_signed_v<T>) {
assert(x >= 0);
return ::tools::bit_width<::tools::make_unsigned_t<T>>(x);
} else {
return ::std::bit_width(x);
}
}
}
#line 1 "tools/countr_zero.hpp"
#line 12 "tools/countr_zero.hpp"
namespace tools {
template <typename T>
constexpr int countr_zero(const T x) noexcept {
static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
if constexpr (::tools::is_signed_v<T>) {
assert(x >= 0);
return ::std::min(::tools::countr_zero<::tools::make_unsigned_t<T>>(x), ::std::numeric_limits<T>::digits);
} else {
return ::std::countr_zero(x);
}
}
}
#line 1 "tools/is_unsigned.hpp"
#line 5 "tools/is_unsigned.hpp"
namespace tools {
template <typename T>
struct is_unsigned : ::std::is_unsigned<T> {};
template <typename T>
inline constexpr bool is_unsigned_v = ::tools::is_unsigned<T>::value;
}
#line 1 "tools/hash_combine.hpp"
#line 6 "tools/hash_combine.hpp"
// Source: https://github.com/google/cityhash/blob/f5dc54147fcce12cefd16548c8e760d68ac04226/src/city.h
// License: MIT
// Author: Google Inc.
// Copyright (c) 2011 Google, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
namespace tools {
template <typename T>
void hash_combine(::std::size_t& seed, const T& v) {
static const ::std::hash<T> hasher;
static constexpr ::std::size_t k_mul = 0x9ddfea08eb382d69ULL;
::std::size_t a = (hasher(v) ^ seed) * k_mul;
a ^= (a >> 47);
::std::size_t b = (seed ^ a) * k_mul;
b ^= (b >> 47);
seed = b * k_mul;
}
}
#line 1 "tools/make_signed.hpp"
#line 5 "tools/make_signed.hpp"
namespace tools {
template <typename T>
struct make_signed : ::std::make_signed<T> {};
template <typename T>
using make_signed_t = typename ::tools::make_signed<T>::type;
}
#line 1 "tools/now.hpp"
#include <chrono>
namespace tools {
inline long long now() {
return ::std::chrono::duration_cast<::std::chrono::nanoseconds>(::std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
}
#line 25 "tools/detail/int128_t.hpp"
namespace tools {
using uint128_t = unsigned __int128;
using int128_t = __int128;
namespace detail {
namespace int128_t {
constexpr ::tools::uint128_t parse_unsigned(const ::std::string_view s) noexcept {
assert(!s.empty());
::tools::uint128_t x = 0;
::std::size_t i = s[0] == '+';
if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
for (i += 2; i < s.size(); ++i) {
assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
x <<= 4;
if ('0' <= s[i] && s[i] <= '9') {
x |= s[i] - '0';
} else if ('a' <= s[i] && s[i] <= 'f') {
x |= s[i] - 'a' + 10;
} else {
x |= s[i] - 'A' + 10;
}
}
} else {
for (; i < s.size(); ++i) {
assert('0' <= s[i] && s[i] <= '9');
x *= 10;
x += s[i] - '0';
}
}
return x;
}
constexpr ::tools::int128_t parse_signed(const ::std::string_view s) noexcept {
assert(!s.empty());
::tools::int128_t x = 0;
if (s[0] == '-') {
::std::size_t i = 1;
if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
for (i += 2; i < s.size(); ++i) {
assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
x *= 16;
if ('0' <= s[i] && s[i] <= '9') {
x -= s[i] - '0';
} else if ('a' <= s[i] && s[i] <= 'f') {
x -= s[i] - 'a' + 10;
} else {
x -= s[i] - 'A' + 10;
}
}
} else {
for (; i < s.size(); ++i) {
assert('0' <= s[i] && s[i] <= '9');
x *= 10;
x -= s[i] - '0';
}
}
} else {
::std::size_t i = s[0] == '+';
if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
for (i += 2; i < s.size(); ++i) {
assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
x <<= 4;
if ('0' <= s[i] && s[i] <= '9') {
x |= s[i] - '0';
} else if ('a' <= s[i] && s[i] <= 'f') {
x |= s[i] - 'a' + 10;
} else {
x |= s[i] - 'A' + 10;
}
}
} else {
for (; i < s.size(); ++i) {
assert('0' <= s[i] && s[i] <= '9');
x *= 10;
x += s[i] - '0';
}
}
}
return x;
}
}
}
constexpr ::tools::uint128_t abs(const ::tools::uint128_t& x) noexcept {
return x;
}
constexpr ::tools::int128_t abs(const ::tools::int128_t& x) {
return x >= 0 ? x : -x;
}
}
#define UINT128_C(c) ::tools::detail::int128_t::parse_unsigned(#c)
#define INT128_C(c) ::tools::detail::int128_t::parse_signed(#c)
inline ::std::istream& operator>>(::std::istream& is, ::tools::uint128_t& x) {
::std::string s;
is >> s;
x = ::tools::detail::int128_t::parse_unsigned(s);
return is;
}
inline ::std::istream& operator>>(::std::istream& is, ::tools::int128_t& x) {
::std::string s;
is >> s;
x = ::tools::detail::int128_t::parse_signed(s);
return is;
}
inline ::std::ostream& operator<<(::std::ostream& os, ::tools::uint128_t x) {
::std::string s;
if (x > 0) {
while (x > 0) {
s.push_back('0' + x % 10);
x /= 10;
}
} else {
s.push_back('0');
}
::std::ranges::reverse(s);
return os << s;
}
inline ::std::ostream& operator<<(::std::ostream& os, ::tools::int128_t x) {
::std::string s;
if (x > 0) {
while (x > 0) {
s.push_back('0' + x % 10);
x /= 10;
}
} else if (x < 0) {
while (x < 0) {
s.push_back('0' + (-(x % 10)));
x /= 10;
}
s.push_back('-');
} else {
s.push_back('0');
}
::std::ranges::reverse(s);
return os << s;
}
#if defined(__GLIBCXX__) && defined(__STRICT_ANSI__)
namespace std {
template <>
struct hash<::tools::uint128_t> {
::std::size_t operator()(const ::tools::uint128_t& x) const {
static const ::std::size_t seed = ::tools::now();
::std::size_t hash = seed;
::tools::hash_combine(hash, static_cast<::std::uint64_t>(x >> 64));
::tools::hash_combine(hash, static_cast<::std::uint64_t>(x & ((UINT128_C(1) << 64) - 1)));
return hash;
}
};
template <>
struct hash<::tools::int128_t> {
::std::size_t operator()(const ::tools::int128_t& x) const {
static ::std::hash<::tools::uint128_t> hasher;
return hasher(static_cast<::tools::uint128_t>(x));
}
};
}
#endif
namespace tools {
template <>
struct is_integral<::tools::int128_t> : ::std::true_type {};
template <>
struct is_integral<::tools::uint128_t> : ::std::true_type {};
template <>
struct is_integral<const ::tools::int128_t> : ::std::true_type {};
template <>
struct is_integral<const ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_integral<volatile ::tools::int128_t> : ::std::true_type {};
template <>
struct is_integral<volatile ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_integral<const volatile ::tools::int128_t> : ::std::true_type {};
template <>
struct is_integral<const volatile ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_signed<::tools::int128_t> : ::std::true_type {};
template <>
struct is_signed<::tools::uint128_t> : ::std::false_type {};
template <>
struct is_signed<const ::tools::int128_t> : ::std::true_type {};
template <>
struct is_signed<const ::tools::uint128_t> : ::std::false_type {};
template <>
struct is_signed<volatile ::tools::int128_t> : ::std::true_type {};
template <>
struct is_signed<volatile ::tools::uint128_t> : ::std::false_type {};
template <>
struct is_signed<const volatile ::tools::int128_t> : ::std::true_type {};
template <>
struct is_signed<const volatile ::tools::uint128_t> : ::std::false_type {};
template <>
struct is_unsigned<::tools::int128_t> : ::std::false_type {};
template <>
struct is_unsigned<::tools::uint128_t> : ::std::true_type {};
template <>
struct is_unsigned<const ::tools::int128_t> : ::std::false_type {};
template <>
struct is_unsigned<const ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_unsigned<volatile ::tools::int128_t> : ::std::false_type {};
template <>
struct is_unsigned<volatile ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_unsigned<const volatile ::tools::int128_t> : ::std::false_type {};
template <>
struct is_unsigned<const volatile ::tools::uint128_t> : ::std::true_type {};
template <>
struct make_signed<::tools::int128_t> {
using type = ::tools::int128_t;
};
template <>
struct make_signed<::tools::uint128_t> {
using type = ::tools::int128_t;
};
template <>
struct make_signed<const ::tools::int128_t> {
using type = const ::tools::int128_t;
};
template <>
struct make_signed<const ::tools::uint128_t> {
using type = const ::tools::int128_t;
};
template <>
struct make_signed<volatile ::tools::int128_t> {
using type = volatile ::tools::int128_t;
};
template <>
struct make_signed<volatile ::tools::uint128_t> {
using type = volatile ::tools::int128_t;
};
template <>
struct make_signed<const volatile ::tools::int128_t> {
using type = const volatile ::tools::int128_t;
};
template <>
struct make_signed<const volatile ::tools::uint128_t> {
using type = const volatile ::tools::int128_t;
};
template <>
struct make_unsigned<::tools::int128_t> {
using type = ::tools::uint128_t;
};
template <>
struct make_unsigned<::tools::uint128_t> {
using type = ::tools::uint128_t;
};
template <>
struct make_unsigned<const ::tools::int128_t> {
using type = const ::tools::uint128_t;
};
template <>
struct make_unsigned<const ::tools::uint128_t> {
using type = const ::tools::uint128_t;
};
template <>
struct make_unsigned<volatile ::tools::int128_t> {
using type = volatile ::tools::uint128_t;
};
template <>
struct make_unsigned<volatile ::tools::uint128_t> {
using type = volatile ::tools::uint128_t;
};
template <>
struct make_unsigned<const volatile ::tools::int128_t> {
using type = const volatile ::tools::uint128_t;
};
template <>
struct make_unsigned<const volatile ::tools::uint128_t> {
using type = const volatile ::tools::uint128_t;
};
#if defined(__GLIBCXX__) && defined(__STRICT_ANSI__)
template <>
constexpr ::tools::uint128_t bit_ceil<::tools::uint128_t>(::tools::uint128_t x) noexcept {
if (x <= 1) return 1;
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
x |= x >> 64;
return ++x;
}
template <>
constexpr ::tools::uint128_t bit_floor<::tools::uint128_t>(::tools::uint128_t x) noexcept {
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
x |= x >> 64;
return x & ~(x >> 1);
}
template <>
constexpr int bit_width<::tools::uint128_t>(::tools::uint128_t x) noexcept {
int w = 0;
if (x & UINT128_C(0xffffffffffffffff0000000000000000)) {
x >>= 64;
w += 64;
}
if (x & UINT128_C(0xffffffff00000000)) {
x >>= 32;
w += 32;
}
if (x & UINT128_C(0xffff0000)) {
x >>= 16;
w += 16;
}
if (x & UINT128_C(0xff00)) {
x >>= 8;
w += 8;
}
if (x & UINT128_C(0xf0)) {
x >>= 4;
w += 4;
}
if (x & UINT128_C(0xc)) {
x >>= 2;
w += 2;
}
if (x & UINT128_C(0x2)) {
x >>= 1;
w += 1;
}
w += x;
return w;
}
namespace detail {
namespace countr_zero {
template <::std::size_t N>
struct ntz_traits;
template <>
struct ntz_traits<128> {
using type = ::tools::uint128_t;
static constexpr int shift = 120;
static constexpr type magic = UINT128_C(0x01061438916347932a5cd9d3ead7b77f);
static constexpr int ntz_table[255] = {
128, 0, 1, -1, 2, -1, 8, -1, 3, -1, 15, -1, 9, -1, 22, -1,
4, -1, 29, -1, 16, -1, 36, -1, 10, -1, 43, -1, 23, -1, 50, -1,
5, -1, 33, -1, 30, -1, 57, -1, 17, -1, 64, -1, 37, -1, 71, -1,
11, -1, 60, -1, 44, -1, 78, -1, 24, -1, 85, -1, 51, -1, 92, -1,
-1, 6, -1, 20, -1, 34, -1, 48, 31, -1, -1, 69, 58, -1, -1, 90,
18, -1, 67, -1, 65, -1, 99, -1, 38, -1, 101, -1, 72, -1, 106, -1,
-1, 12, -1, 40, -1, 61, -1, 82, 45, -1, -1, 103, 79, -1, 113, -1,
-1, 25, -1, 74, 86, -1, -1, 116, -1, 52, -1, 108, -1, 93, -1, 120,
127, -1, -1, 7, -1, 14, -1, 21, -1, 28, -1, 35, -1, 42, -1, 49,
-1, 32, -1, 56, -1, 63, -1, 70, -1, 59, -1, 77, -1, 84, -1, 91,
-1, 19, -1, 47, -1, 68, -1, 89, -1, 66, -1, 98, -1, 100, -1, 105,
-1, 39, -1, 81, -1, 102, -1, 112, -1, 73, -1, 115, -1, 107, -1, 119,
126, -1, 13, -1, 27, -1, 41, -1, -1, 55, 62, -1, -1, 76, 83, -1,
-1, 46, -1, 88, -1, 97, -1, 104, -1, 80, -1, 111, -1, 114, -1, 118,
125, -1, 26, -1, 54, -1, 75, -1, -1, 87, 96, -1, -1, 110, -1, 117,
124, -1, 53, -1, -1, 95, 109, -1, 123, -1, 94, -1, 122, -1, 121
};
};
template <typename T>
constexpr int impl(const T x) noexcept {
using tr = ::tools::detail::countr_zero::ntz_traits<::std::numeric_limits<T>::digits>;
using type = typename tr::type;
return tr::ntz_table[static_cast<type>(tr::magic * static_cast<type>(x & -x)) >> tr::shift];
}
}
}
template <>
constexpr int countr_zero<::tools::uint128_t>(const ::tools::uint128_t x) noexcept {
return ::tools::detail::countr_zero::impl(x);
}
#endif
}
#line 5 "tools/uint128_t.hpp"
#line 11 "tools/popcount.hpp"
namespace tools {
template <typename T>
constexpr int popcount(T) noexcept;
template <typename T>
constexpr int popcount(const T x) noexcept {
static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
if constexpr (::tools::is_signed_v<T>) {
assert(x >= 0);
return ::tools::popcount<::tools::make_unsigned_t<T>>(x);
} else {
return ::std::popcount(x);
}
}
#if defined(__GLIBCXX__) && defined(__STRICT_ANSI__)
template <>
constexpr int popcount<::tools::uint128_t>(::tools::uint128_t x) noexcept {
x = (x & UINT128_C(0x55555555555555555555555555555555)) + (x >> 1 & UINT128_C(0x55555555555555555555555555555555));
x = (x & UINT128_C(0x33333333333333333333333333333333)) + (x >> 2 & UINT128_C(0x33333333333333333333333333333333));
x = (x & UINT128_C(0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f)) + (x >> 4 & UINT128_C(0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f));
x = (x & UINT128_C(0x00ff00ff00ff00ff00ff00ff00ff00ff)) + (x >> 8 & UINT128_C(0x00ff00ff00ff00ff00ff00ff00ff00ff));
x = (x & UINT128_C(0x0000ffff0000ffff0000ffff0000ffff)) + (x >> 16 & UINT128_C(0x0000ffff0000ffff0000ffff0000ffff));
x = (x & UINT128_C(0x00000000ffffffff00000000ffffffff)) + (x >> 32 & UINT128_C(0x00000000ffffffff00000000ffffffff));
x = (x & UINT128_C(0x0000000000000000ffffffffffffffff)) + (x >> 64 & UINT128_C(0x0000000000000000ffffffffffffffff));
return x;
}
#endif
}
#line 1 "tools/chmin.hpp"
#line 6 "tools/chmin.hpp"
namespace tools {
template <typename M, typename N>
bool chmin(M& lhs, const N& rhs) {
bool updated;
if constexpr (::std::is_integral_v<M> && ::std::is_integral_v<N>) {
updated = ::std::cmp_less(rhs, lhs);
} else {
updated = rhs < lhs;
}
if (updated) lhs = rhs;
return updated;
}
}
#line 10 "tests/prim/unconnected.test.cpp"
using ll = long long;
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
ll N, M;
std::cin >> N >> M;
std::vector<ll> X(N), Y(N);
for (auto& X_i : X) std::cin >> X_i;
for (auto& Y_i : Y) std::cin >> Y_i;
std::vector<ll> A(M), B(M), Z(M);
for (ll i = 0; i < M; ++i) {
std::cin >> A[i] >> B[i] >> Z[i];
--A[i], --B[i];
}
ll answer = std::numeric_limits<ll>::max();
for (ll state = 0; state < 4; ++state) {
tools::prim<ll> graph(N + tools::popcount(state));
if (state & 1) {
for (ll i = 0; i < N; ++i) {
graph.add_edge(i, N, X[i]);
}
}
if (state & 2) {
for (ll i = 0; i < N; ++i) {
graph.add_edge(i, N + (state & 1), Y[i]);
}
}
for (ll i = 0; i < M; ++i) {
graph.add_edge(A[i], B[i], Z[i]);
}
if (const auto groups = graph.query().first; groups.size() == 1) {
tools::chmin(answer, groups[0].first);
}
}
std::cout << answer << '\n';
return 0;
}