1
+ /*
2
+ written by Pankaj Kumar.
3
+ country:-INDIA
4
+ Institute: National Institute of Technology, Uttarakhand
5
+ */
6
+ #include < bits/stdc++.h>
7
+ #include < ext/pb_ds/assoc_container.hpp>
8
+ #include < ext/pb_ds/tree_policy.hpp>
9
+ using namespace std ;
10
+ using namespace __gnu_pbds ;
11
+ typedef long long ll ;
12
+ typedef unsigned long long ull;
13
+ typedef vector<ll> vl;
14
+ #define speed cin.tie(0 );cout.tie(0 );ios_base::sync_with_stdio(0 );
15
+ /* Abbrevations */
16
+ #define ff first
17
+ #define ss second
18
+ #define mp make_pair
19
+ #define line cout<<endl;
20
+ #define pb push_back
21
+ #define Endl " \n "
22
+ // loops
23
+ #define forin (arr,n ) for (ll i=0 ;i<n;i++) cin>>arr[i];
24
+ // Some print
25
+ #define no cout<<" NO" <<endl;
26
+ #define yes cout<<" YES" <<endl;
27
+ // sort
28
+ #define all (V ) (V).begin(),(V).end()
29
+ #define srt (V ) sort(all(V))
30
+ #define srtGreat (V ) sort(all(V),greater<ll>())
31
+ // some extra
32
+ #define printv (v ) for (ll i=0 ;i<ll(v.size());i++){cout<<v[i]<<" " ;} line;
33
+ #define precision (x ) cout<<fixed<<setprecision(x);
34
+ #define sz (V ) ll(V.size())
35
+ // template
36
+ template <typename T>
37
+ T mymax (T x,T y)
38
+ {
39
+ return (x>y)?x:y;
40
+ }
41
+ // function
42
+ ll power (ll x,ll y,ll mod)
43
+ {
44
+ ll res=1 ;
45
+ // x=x%mod;
46
+ while (y>0 )
47
+ {
48
+ if (y%2 ==1 )
49
+ {
50
+ res*=x;
51
+ // res=res%mod;
52
+ }
53
+ y/=2 ; x*=x; // x=x%mod;
54
+ }
55
+ return res;
56
+ }
57
+ ll str_to_num (string s)
58
+ {
59
+ stringstream pk (s);
60
+ ll num;
61
+ pk>>num;
62
+ return num;
63
+ }
64
+
65
+ string num_to_str (ll num)
66
+ {
67
+ return to_string (num);
68
+ }
69
+ // datatype definination
70
+ #define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
71
+ class Point
72
+ {
73
+ public:
74
+ ll x;
75
+ ll y;
76
+ ll z;
77
+ ll getsum ()
78
+ {
79
+ return x+y+z;
80
+ }
81
+ };
82
+ /* ascii value
83
+ A=65,Z=90,a=97,z=122
84
+ */
85
+ /* --------------------MAIN PROGRAM----------------------------*/
86
+ // to run ctrl+b
87
+ const ll INF=LONG_MAX;
88
+ const ll mod1=1e9 +7 ;
89
+ const ll mod2=998244353 ;
90
+
91
+
92
+ // Techniques
93
+ // divide into cases, brute force, pattern finding
94
+ // sort, greedy, binary search, two pointer
95
+ // transform into graph
96
+
97
+ ll solve ()
98
+ {
99
+ ll n;
100
+ cin>>n;
101
+ vl a (n),b (n);
102
+ forin (a,n);
103
+ forin (b,n);
104
+ vl from_begin (n),from_end (n);
105
+ vector<vl> dp (5001 ,vl (5001 ,0 ));
106
+ ll maxo=0 ;
107
+ ll pro=0 ;
108
+ for (ll i=0 ;i<n;i++){
109
+ ll temp=a[i]*b[i];
110
+ pro+=temp;
111
+ from_begin[i]=pro;
112
+ }
113
+ pro=0 ;
114
+ for (ll i=n-1 ;i>=0 ;i--){
115
+ ll temp=a[i]*b[i];
116
+ pro+=temp;
117
+ from_end[i]=pro;
118
+ }
119
+ for (ll length=1 ;length<=n;length++){
120
+ for (ll i=0 ;i<n;i++){
121
+ ll j=i+length;
122
+ if (j>=n)
123
+ break ;
124
+ if (length==1 )
125
+ dp[i][j]=a[i]*b[j]+a[j]*b[i];
126
+ else if (length==2 )
127
+ dp[i][j]=a[i]*b[i+2 ]+a[i+1 ]*b[i+1 ]+a[i+2 ]*b[i];
128
+ else
129
+ dp[i][j]=dp[i+1 ][j-1 ]+a[i]*b[j]+a[j]*b[i];
130
+ }
131
+ }
132
+ maxo=from_begin[n-1 ];
133
+ for (ll i=0 ;i<n;i++){
134
+ for (ll j=i+1 ;j<n;j++){
135
+ ll subarray_sum=dp[i][j];
136
+ if (i>0 )
137
+ subarray_sum+=from_begin[i-1 ];
138
+ if (j<(n-1 ))
139
+ subarray_sum+=from_end[j+1 ];
140
+ maxo=max (maxo,subarray_sum);
141
+ }
142
+ }
143
+ cout<<maxo<<endl;
144
+ return 0 ;
145
+ }
146
+
147
+ int main ()
148
+ {
149
+ speed;
150
+ /* #ifndef ONLINE_JUDGE
151
+ freopen("input.txt","r",stdin);
152
+ freopen("output.txt","w",stdout);
153
+ #endif */
154
+ ll TestCase=1 ;
155
+ // cin>>TestCase;
156
+ while (TestCase--)
157
+ {
158
+ solve ();
159
+ }
160
+ }
161
+ /* -----------------END OF PROGRAM --------------------*/
162
+ /*
163
+ * stuff you should look before submission
164
+ * constraint and time limit
165
+ * int overflow
166
+ * special test case (n=0||n=1||n=2)
167
+ * don't get stuck on one approach if you get wrong answer
168
+ */
0 commit comments