#include<bits/stdc++.h>
using namespace std ;
#define maxn 200009
#define ll long long
#define fi first
#define se second
#define pb push_back
#define left id<<1
#define right id<<1|1
#define re exit(0);
const int mod = 1e9+7 ;
const int INF = 1e9 ;
const int LOG = 18 ;
typedef vector<int> vi ;
typedef vector<ll> vl ;
typedef pair<int,int> pii ;
typedef vector<pii> vii ;
typedef pair<ll,ll> pll ;
void add ( int &a , int b )
{
a += b ;
if ( a < 0 ) a += mod ;
if ( a >= mod ) a -= mod ;
}
template < typename T > void chkmin (T &a , T b) { if (a>b) a=b ;}
template < typename T > void chkmax (T &a , T b) { if (a<b) a=b ;}
void rf ()
{
freopen ("bai1.inp","r",stdin) ;
// freopen ("bai1.out","w",stdout) ;
}
int _pow ( int a , int n )
{
if ( n == 0 ) return 1 ;
int res = _pow(a,n/2) ;
if ( n % 2 ) return (1ll*res*res%mod*a%mod) ;
else return 1ll*res*res%mod ;
}
int n , m ;
int a [maxn] , b [maxn] ;
ll dp [2009][2009] ;
void sub1 ()
{
memset ( dp , 0x3f , sizeof dp ) ;
dp [1][1] = 0 ;
for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= m ; j ++ )
{
if ( i == 1 && j == 1 ) continue ;
if ( j > 1 ) chkmin (dp[i][j],dp[i][j-1]+a[i]) ;
if ( i > 1 ) chkmin (dp[i][j],dp[i-1][j]+b[j]) ;
}
cout << dp [n][m] ;
}
int row [maxn] , col [maxn] ;
int lef [maxn] , rig [maxn] ;
bool used [maxn] ;
void sub2 ()
{
memset ( lef , 0x3f , sizeof lef ) ;
memset ( rig , 0x3f , sizeof rig ) ;
memset ( used , false , sizeof used ) ;
int num_r = 0 ;
for ( int i = 1 ; i <= n ; i ++ ) lef [i] = lef [i-1] , chkmin (lef[i],a[i]) ;
for ( int i = n ; i >= 1 ; i -- ) rig [i] = rig [i+1] , chkmin (rig[i],a[i]) ;
for ( int i = 2 ; i < n ; i ++ ) if ( a [i] >= lef [i-1] && a [i] >= rig [i+1] ) used [i] = true ;
for ( int i = 1 ; i <= n ; i ++ )
{
if ( used [i] == false ) row [++num_r] = i ;
}
memset ( lef , 0x3f , sizeof lef ) ;
memset ( rig , 0x3f , sizeof rig ) ;
memset ( used , false , sizeof used ) ;
int num_c = 0 ;
for ( int i = 1 ; i <= m ; i ++ ) lef [i] = lef [i-1] , chkmin (lef[i],b[i]) ;
for ( int i = m ; i >= 1 ; i -- ) rig [i] = rig [i+1] , chkmin (rig[i],b[i]) ;
for ( int i = 2 ; i < m ; i ++ ) if ( b [i] >= lef [i-1] && b [i] >= rig [i+1] ) used [i] = true ;
for ( int i = 1 ; i <= m ; i ++ )
{
if ( used [i] == false ) col [++num_c] = i ;
}
memset ( dp , 0x3f , sizeof dp ) ;
dp [1][1] = 0 ;
for ( int i = 1 ; i <= num_r ; i ++ )
{
for ( int j = 1 ; j <= num_c ; j ++ )
{
if ( i == 1 && j == 1 ) continue ;
if ( j > 1 ) chkmin (dp[i][j],dp[i][j-1]+1ll*(col[j]-col[j-1])*a[row[i]]) ;
if ( i > 1 ) chkmin (dp[i][j],dp[i-1][j]+1ll*(row[i]-row[i-1])*b[col[j]]) ;
}
}
cout << dp [num_r][num_c] ;
}
int main ()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ; cout.tie(0) ;
// rf () ;
cin >> n >> m ;
for ( int i = 1 ; i <= n ; i ++ ) cin >> a [i] ;
for ( int i = 1 ; i <= m ; i ++ ) cin >> b [i] ;
if ( n <= 1000 && m <= 1000 ) sub1 () ;
else sub2 () ;
}
Compilation message
kyoto.cpp: In function 'void rf()':
kyoto.cpp:34:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen ("bai1.inp","r",stdin) ;
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
33116 KB |
Output is correct |
2 |
Correct |
5 ms |
33116 KB |
Output is correct |
3 |
Correct |
4 ms |
33284 KB |
Output is correct |
4 |
Correct |
4 ms |
33116 KB |
Output is correct |
5 |
Correct |
4 ms |
33116 KB |
Output is correct |
6 |
Correct |
4 ms |
33304 KB |
Output is correct |
7 |
Correct |
5 ms |
33268 KB |
Output is correct |
8 |
Correct |
4 ms |
33116 KB |
Output is correct |
9 |
Correct |
8 ms |
33116 KB |
Output is correct |
10 |
Correct |
8 ms |
33264 KB |
Output is correct |
11 |
Correct |
8 ms |
33220 KB |
Output is correct |
12 |
Correct |
8 ms |
33112 KB |
Output is correct |
13 |
Correct |
7 ms |
33116 KB |
Output is correct |
14 |
Correct |
8 ms |
33528 KB |
Output is correct |
15 |
Correct |
8 ms |
33112 KB |
Output is correct |
16 |
Correct |
7 ms |
33112 KB |
Output is correct |
17 |
Correct |
7 ms |
33116 KB |
Output is correct |
18 |
Correct |
4 ms |
33116 KB |
Output is correct |
19 |
Correct |
4 ms |
33112 KB |
Output is correct |
20 |
Correct |
4 ms |
33116 KB |
Output is correct |
21 |
Correct |
4 ms |
33116 KB |
Output is correct |
22 |
Correct |
4 ms |
33256 KB |
Output is correct |
23 |
Correct |
4 ms |
33116 KB |
Output is correct |
24 |
Correct |
4 ms |
33284 KB |
Output is correct |
25 |
Correct |
4 ms |
33116 KB |
Output is correct |
26 |
Correct |
4 ms |
33112 KB |
Output is correct |
27 |
Correct |
4 ms |
33112 KB |
Output is correct |
28 |
Correct |
4 ms |
33116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
33116 KB |
Output is correct |
2 |
Correct |
4 ms |
33244 KB |
Output is correct |
3 |
Correct |
6 ms |
35420 KB |
Output is correct |
4 |
Correct |
9 ms |
35928 KB |
Output is correct |
5 |
Correct |
9 ms |
35932 KB |
Output is correct |
6 |
Correct |
6 ms |
35676 KB |
Output is correct |
7 |
Correct |
18 ms |
36700 KB |
Output is correct |
8 |
Correct |
16 ms |
36700 KB |
Output is correct |
9 |
Correct |
16 ms |
36700 KB |
Output is correct |
10 |
Correct |
15 ms |
36812 KB |
Output is correct |
11 |
Correct |
26 ms |
36700 KB |
Output is correct |
12 |
Correct |
16 ms |
36696 KB |
Output is correct |
13 |
Correct |
16 ms |
36696 KB |
Output is correct |
14 |
Correct |
26 ms |
36856 KB |
Output is correct |
15 |
Correct |
16 ms |
36956 KB |
Output is correct |
16 |
Correct |
4 ms |
33116 KB |
Output is correct |
17 |
Correct |
4 ms |
33272 KB |
Output is correct |
18 |
Correct |
4 ms |
33116 KB |
Output is correct |
19 |
Correct |
4 ms |
33116 KB |
Output is correct |
20 |
Correct |
4 ms |
33228 KB |
Output is correct |
21 |
Correct |
5 ms |
33120 KB |
Output is correct |
22 |
Correct |
4 ms |
33116 KB |
Output is correct |
23 |
Correct |
4 ms |
33116 KB |
Output is correct |
24 |
Correct |
4 ms |
33116 KB |
Output is correct |
25 |
Correct |
4 ms |
33268 KB |
Output is correct |
26 |
Correct |
4 ms |
33116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
33116 KB |
Output is correct |
2 |
Correct |
5 ms |
33116 KB |
Output is correct |
3 |
Correct |
4 ms |
33284 KB |
Output is correct |
4 |
Correct |
4 ms |
33116 KB |
Output is correct |
5 |
Correct |
4 ms |
33116 KB |
Output is correct |
6 |
Correct |
4 ms |
33304 KB |
Output is correct |
7 |
Correct |
5 ms |
33268 KB |
Output is correct |
8 |
Correct |
4 ms |
33116 KB |
Output is correct |
9 |
Correct |
8 ms |
33116 KB |
Output is correct |
10 |
Correct |
8 ms |
33264 KB |
Output is correct |
11 |
Correct |
8 ms |
33220 KB |
Output is correct |
12 |
Correct |
8 ms |
33112 KB |
Output is correct |
13 |
Correct |
7 ms |
33116 KB |
Output is correct |
14 |
Correct |
8 ms |
33528 KB |
Output is correct |
15 |
Correct |
8 ms |
33112 KB |
Output is correct |
16 |
Correct |
7 ms |
33112 KB |
Output is correct |
17 |
Correct |
7 ms |
33116 KB |
Output is correct |
18 |
Correct |
4 ms |
33116 KB |
Output is correct |
19 |
Correct |
4 ms |
33112 KB |
Output is correct |
20 |
Correct |
4 ms |
33116 KB |
Output is correct |
21 |
Correct |
4 ms |
33116 KB |
Output is correct |
22 |
Correct |
4 ms |
33256 KB |
Output is correct |
23 |
Correct |
4 ms |
33116 KB |
Output is correct |
24 |
Correct |
4 ms |
33284 KB |
Output is correct |
25 |
Correct |
4 ms |
33116 KB |
Output is correct |
26 |
Correct |
4 ms |
33112 KB |
Output is correct |
27 |
Correct |
4 ms |
33112 KB |
Output is correct |
28 |
Correct |
4 ms |
33116 KB |
Output is correct |
29 |
Correct |
4 ms |
33116 KB |
Output is correct |
30 |
Correct |
4 ms |
33244 KB |
Output is correct |
31 |
Correct |
6 ms |
35420 KB |
Output is correct |
32 |
Correct |
9 ms |
35928 KB |
Output is correct |
33 |
Correct |
9 ms |
35932 KB |
Output is correct |
34 |
Correct |
6 ms |
35676 KB |
Output is correct |
35 |
Correct |
18 ms |
36700 KB |
Output is correct |
36 |
Correct |
16 ms |
36700 KB |
Output is correct |
37 |
Correct |
16 ms |
36700 KB |
Output is correct |
38 |
Correct |
15 ms |
36812 KB |
Output is correct |
39 |
Correct |
26 ms |
36700 KB |
Output is correct |
40 |
Correct |
16 ms |
36696 KB |
Output is correct |
41 |
Correct |
16 ms |
36696 KB |
Output is correct |
42 |
Correct |
26 ms |
36856 KB |
Output is correct |
43 |
Correct |
16 ms |
36956 KB |
Output is correct |
44 |
Correct |
4 ms |
33116 KB |
Output is correct |
45 |
Correct |
4 ms |
33272 KB |
Output is correct |
46 |
Correct |
4 ms |
33116 KB |
Output is correct |
47 |
Correct |
4 ms |
33116 KB |
Output is correct |
48 |
Correct |
4 ms |
33228 KB |
Output is correct |
49 |
Correct |
5 ms |
33120 KB |
Output is correct |
50 |
Correct |
4 ms |
33116 KB |
Output is correct |
51 |
Correct |
4 ms |
33116 KB |
Output is correct |
52 |
Correct |
4 ms |
33116 KB |
Output is correct |
53 |
Correct |
4 ms |
33268 KB |
Output is correct |
54 |
Correct |
4 ms |
33116 KB |
Output is correct |
55 |
Correct |
12 ms |
36700 KB |
Output is correct |
56 |
Correct |
5 ms |
35416 KB |
Output is correct |
57 |
Correct |
5 ms |
35412 KB |
Output is correct |
58 |
Correct |
7 ms |
35676 KB |
Output is correct |
59 |
Correct |
22 ms |
37980 KB |
Output is correct |
60 |
Correct |
21 ms |
38004 KB |
Output is correct |
61 |
Correct |
21 ms |
37980 KB |
Output is correct |
62 |
Correct |
21 ms |
37976 KB |
Output is correct |
63 |
Runtime error |
577 ms |
76368 KB |
Execution killed with signal 11 |
64 |
Halted |
0 ms |
0 KB |
- |