#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("trapv")
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
using namespace std;
///~~~~~~~~~~~~~~~~~~~~~~~~~~
template <typename H, typename T>
ostream& operator<<(ostream& os, pair<H, T> m){
return os <<"("<< m.st<<", "<<m.nd<<")";
}
template <typename H>
ostream& operator<<(ostream& os, vector<H> V){
os<<"{";
for(int i=0; i<V.size(); i++){
if(i)os<<", ";
os<<V[i];
}
os<<"}";
return os;
}
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);
//#define deb(x...) ;
///~~~~~~~~~~~~~~~~~~~~~~~~~
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int K=19, N=1<<K, INF=1e9+5, mod2=1e9+7, mod=998244353;
struct modint{
int n=0;
modint(){}
modint(ll x){
n=x%mod;
if(n<0)n+=mod;
}
operator int(){
return n;
}
modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;}
modint operator+=(modint a){return (*this)=(*this)+a;}
modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;}
modint operator-=(modint a){return (*this)=(*this)-a;}
modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;}
modint operator*=(modint a){return (*this)=(*this)*a;}
modint operator^(const ll &m)const{
modint a(1);
if(m==0)return a;
if(m==1)return (*this);
a=(*this)^(m/2);
a*=a;
return a*((*this)^(m&1));
}
modint odw(){
return (*this)^((ll)mod-2);
}
modint operator/(modint a){return (*this)*a.odw();}
modint operator/=(modint a){return (*this)=(*this)/a;}
bool operator==(modint a){return a.n==n;}
friend ostream& operator<<(ostream& os, modint m) {
return os << m.n;
}
};
modint fact[N], fact2[N];
typedef vector<modint> vm;
void factinit(){
fact[0]=1;
for(int i=1; i<N; i++){
fact[i]=(fact[i-1]*modint(i))%mod;
}
fact2[N-1]=fact[N-1].odw();
for(int i=N-2; i>=0; i--){
fact2[i]=(fact2[i+1]*modint(i+1))%mod;
}
}
modint npok(int _n, int _k){
if(_n<_k)return 0;
return fact[_n]*fact2[_k]*fact2[_n-_k];
}
int los(int a, int b){
return a+(rng())%(b-a+1);
}
int main(){
//factinit();
//BOOST
int n, m;
cin>>n>>m;
vi A(n), B(m);
for(int &i:A)cin>>i;
for(int &i:B)cin>>i;
vii V, V2;
for(int i=0; i<n; i++){
if(V.size()<=1){
V.eb(i, A[i]);
}
else{
while(V.size()>1){
if((V.end()[-1].st-V.end()[-2].st)*1ll*A[i]+(i-V.end()[-1].st)*1ll*V.end()[-2].nd
<=V.end()[-1].nd*(i-V.end()[-2].st)){
V.pp();
}
else break;
}
V.eb(i, A[i]);
}
}
for(int i=0; i<m; i++){
if(V2.size()<=1){
V2.eb(i, B[i]);
}
else{
while(V2.size()>1){
//deb((V.end()[-1].st-V.end()[-2].st)*1ll*(V.end()[-1].nd-A[i]));
if((V2.end()[-1].st-V2.end()[-2].st)*1ll*B[i]+(i-V2.end()[-1].st)*1ll*V2.end()[-2].nd
<=V2.end()[-1].nd*(i-V2.end()[-2].st)){
V2.pp();
}
else break;
}
V2.eb(i, B[i]);
}
}
vector<vector<ll> > dp(2, vector<ll>(V2.size(), 1e18));
dp[0][0]=0;
vector<ll> cost(V.size()), cost2(V2.size());
for(int i=1; i<V.size(); i++){
cost[i]=V[i].st-V[i-1].st;
}
for(int i=1; i<V2.size(); i++){
cost2[i]=V2[i].st-V2[i-1].st;
}
for(int i=0; i<V.size(); i++){
bool par=i&1, par2=par^1;
for(int j=0; j<V2.size(); j++){
if(i||j)dp[par][j]=1e18;
if(i)dp[par][j]=min(dp[par][j], dp[par2][j]+V2[j].nd*cost[i]);
if(j)dp[par][j]=min(dp[par][j], dp[par][j-1]+V[i].nd*cost2[j]);
//deb(i, j, dp[i&1][j]);
}
}
cout<<dp[(V.size()&1)^1][V2.size()-1];
}
Compilation message
kyoto.cpp: In function 'int main()':
kyoto.cpp:165:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
165 | for(int i=1; i<V.size(); i++){
| ~^~~~~~~~~
kyoto.cpp:168:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
168 | for(int i=1; i<V2.size(); i++){
| ~^~~~~~~~~~
kyoto.cpp:171:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
171 | for(int i=0; i<V.size(); i++){
| ~^~~~~~~~~
kyoto.cpp:173:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
173 | for(int j=0; j<V2.size(); j++){
| ~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4404 KB |
Output is correct |
2 |
Correct |
4 ms |
4308 KB |
Output is correct |
3 |
Correct |
4 ms |
4392 KB |
Output is correct |
4 |
Correct |
4 ms |
4436 KB |
Output is correct |
5 |
Correct |
4 ms |
4308 KB |
Output is correct |
6 |
Correct |
4 ms |
4308 KB |
Output is correct |
7 |
Correct |
4 ms |
4308 KB |
Output is correct |
8 |
Correct |
4 ms |
4328 KB |
Output is correct |
9 |
Correct |
5 ms |
4436 KB |
Output is correct |
10 |
Correct |
5 ms |
4436 KB |
Output is correct |
11 |
Correct |
5 ms |
4436 KB |
Output is correct |
12 |
Correct |
5 ms |
4436 KB |
Output is correct |
13 |
Correct |
8 ms |
4436 KB |
Output is correct |
14 |
Correct |
5 ms |
4436 KB |
Output is correct |
15 |
Correct |
5 ms |
4436 KB |
Output is correct |
16 |
Correct |
5 ms |
4436 KB |
Output is correct |
17 |
Correct |
5 ms |
4432 KB |
Output is correct |
18 |
Correct |
4 ms |
4308 KB |
Output is correct |
19 |
Correct |
4 ms |
4404 KB |
Output is correct |
20 |
Correct |
4 ms |
4380 KB |
Output is correct |
21 |
Correct |
4 ms |
4404 KB |
Output is correct |
22 |
Correct |
4 ms |
4308 KB |
Output is correct |
23 |
Correct |
4 ms |
4308 KB |
Output is correct |
24 |
Correct |
4 ms |
4344 KB |
Output is correct |
25 |
Correct |
4 ms |
4308 KB |
Output is correct |
26 |
Correct |
4 ms |
4408 KB |
Output is correct |
27 |
Correct |
4 ms |
4308 KB |
Output is correct |
28 |
Correct |
3 ms |
4308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4404 KB |
Output is correct |
2 |
Correct |
4 ms |
4412 KB |
Output is correct |
3 |
Correct |
4 ms |
4308 KB |
Output is correct |
4 |
Correct |
16 ms |
4948 KB |
Output is correct |
5 |
Correct |
15 ms |
4948 KB |
Output is correct |
6 |
Correct |
9 ms |
4564 KB |
Output is correct |
7 |
Correct |
39 ms |
5940 KB |
Output is correct |
8 |
Correct |
35 ms |
5944 KB |
Output is correct |
9 |
Correct |
38 ms |
5932 KB |
Output is correct |
10 |
Correct |
35 ms |
5928 KB |
Output is correct |
11 |
Correct |
32 ms |
5836 KB |
Output is correct |
12 |
Correct |
34 ms |
5940 KB |
Output is correct |
13 |
Correct |
35 ms |
5844 KB |
Output is correct |
14 |
Correct |
33 ms |
5944 KB |
Output is correct |
15 |
Correct |
39 ms |
6076 KB |
Output is correct |
16 |
Correct |
4 ms |
4308 KB |
Output is correct |
17 |
Correct |
4 ms |
4308 KB |
Output is correct |
18 |
Correct |
4 ms |
4408 KB |
Output is correct |
19 |
Correct |
4 ms |
4412 KB |
Output is correct |
20 |
Correct |
4 ms |
4408 KB |
Output is correct |
21 |
Correct |
4 ms |
4380 KB |
Output is correct |
22 |
Correct |
3 ms |
4308 KB |
Output is correct |
23 |
Correct |
5 ms |
4588 KB |
Output is correct |
24 |
Correct |
4 ms |
4308 KB |
Output is correct |
25 |
Correct |
5 ms |
4308 KB |
Output is correct |
26 |
Correct |
4 ms |
4308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4404 KB |
Output is correct |
2 |
Correct |
4 ms |
4308 KB |
Output is correct |
3 |
Correct |
4 ms |
4392 KB |
Output is correct |
4 |
Correct |
4 ms |
4436 KB |
Output is correct |
5 |
Correct |
4 ms |
4308 KB |
Output is correct |
6 |
Correct |
4 ms |
4308 KB |
Output is correct |
7 |
Correct |
4 ms |
4308 KB |
Output is correct |
8 |
Correct |
4 ms |
4328 KB |
Output is correct |
9 |
Correct |
5 ms |
4436 KB |
Output is correct |
10 |
Correct |
5 ms |
4436 KB |
Output is correct |
11 |
Correct |
5 ms |
4436 KB |
Output is correct |
12 |
Correct |
5 ms |
4436 KB |
Output is correct |
13 |
Correct |
8 ms |
4436 KB |
Output is correct |
14 |
Correct |
5 ms |
4436 KB |
Output is correct |
15 |
Correct |
5 ms |
4436 KB |
Output is correct |
16 |
Correct |
5 ms |
4436 KB |
Output is correct |
17 |
Correct |
5 ms |
4432 KB |
Output is correct |
18 |
Correct |
4 ms |
4308 KB |
Output is correct |
19 |
Correct |
4 ms |
4404 KB |
Output is correct |
20 |
Correct |
4 ms |
4380 KB |
Output is correct |
21 |
Correct |
4 ms |
4404 KB |
Output is correct |
22 |
Correct |
4 ms |
4308 KB |
Output is correct |
23 |
Correct |
4 ms |
4308 KB |
Output is correct |
24 |
Correct |
4 ms |
4344 KB |
Output is correct |
25 |
Correct |
4 ms |
4308 KB |
Output is correct |
26 |
Correct |
4 ms |
4408 KB |
Output is correct |
27 |
Correct |
4 ms |
4308 KB |
Output is correct |
28 |
Correct |
3 ms |
4308 KB |
Output is correct |
29 |
Correct |
4 ms |
4404 KB |
Output is correct |
30 |
Correct |
4 ms |
4412 KB |
Output is correct |
31 |
Correct |
4 ms |
4308 KB |
Output is correct |
32 |
Correct |
16 ms |
4948 KB |
Output is correct |
33 |
Correct |
15 ms |
4948 KB |
Output is correct |
34 |
Correct |
9 ms |
4564 KB |
Output is correct |
35 |
Correct |
39 ms |
5940 KB |
Output is correct |
36 |
Correct |
35 ms |
5944 KB |
Output is correct |
37 |
Correct |
38 ms |
5932 KB |
Output is correct |
38 |
Correct |
35 ms |
5928 KB |
Output is correct |
39 |
Correct |
32 ms |
5836 KB |
Output is correct |
40 |
Correct |
34 ms |
5940 KB |
Output is correct |
41 |
Correct |
35 ms |
5844 KB |
Output is correct |
42 |
Correct |
33 ms |
5944 KB |
Output is correct |
43 |
Correct |
39 ms |
6076 KB |
Output is correct |
44 |
Correct |
4 ms |
4308 KB |
Output is correct |
45 |
Correct |
4 ms |
4308 KB |
Output is correct |
46 |
Correct |
4 ms |
4408 KB |
Output is correct |
47 |
Correct |
4 ms |
4412 KB |
Output is correct |
48 |
Correct |
4 ms |
4408 KB |
Output is correct |
49 |
Correct |
4 ms |
4380 KB |
Output is correct |
50 |
Correct |
3 ms |
4308 KB |
Output is correct |
51 |
Correct |
5 ms |
4588 KB |
Output is correct |
52 |
Correct |
4 ms |
4308 KB |
Output is correct |
53 |
Correct |
5 ms |
4308 KB |
Output is correct |
54 |
Correct |
4 ms |
4308 KB |
Output is correct |
55 |
Correct |
54 ms |
6276 KB |
Output is correct |
56 |
Correct |
4 ms |
4420 KB |
Output is correct |
57 |
Correct |
6 ms |
4548 KB |
Output is correct |
58 |
Correct |
109 ms |
5028 KB |
Output is correct |
59 |
Execution timed out |
2064 ms |
8964 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |