#include<iostream>
#include<stdio.h>
#include<stdint.h>
#include<vector>
#include<algorithm>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>
using namespace std;
const int64_t INF = 1e9 + 777;
const int64_t mINF = 1e9;
const int64_t MOD = 998244353;
const int nbit = 11;
const int ndig = 10;
const int nchar = 26;
const int D = 4;
int dr[D] = {0, 1, 0, -1};
int dc[D] = {1, 0, -1, 0};
struct Thing
{
int64_t val;
int i;
int j;
int t;
Thing(int64_t val_, int i_, int j_, int t_) : val(val_), i(i_), j(j_), t(t_) {}
bool operator < (const Thing& o) const
{
int64_t len1 = j - i;
int64_t len2 = o.j - o.i;
if(val * len2 != o.val * len1) return (val * len2) > (o.val * len1);
if(t != o.t) return t > o.t;
if(i != o.i) return i > o.i;
return j > o.j;
}
};
struct Solution
{
int64_t ans;
set<int> ai;
set<int> bi;
set<Thing> s;
vector<int64_t> a;
vector<int64_t> b;
Solution() {}
void solve()
{
int nr;
int nc;
cin >> nr >> nc;
a.resize(nr);
b.resize(nc);
for(int i = 0; i < nr; i++)
{
cin >> a[i];
}
for(int i = 0; i < nc; i++)
{
cin >> b[i];
}
//t = 0 = a // t = 1 = b
for(int i = 1; i < nr; i++)
{
s.insert(Thing(a[i] - a[i - 1], i - 1, i, 0));
}
for(int i = 1; i < nc; i++)
{
s.insert(Thing(b[i] - b[i - 1], i - 1, i, 1));
}
s.insert(Thing(-INF, -1, -2, - 1));
for(int i = 0; i < nr; i++)
{
ai.insert(i);
}
for(int i = 0; i < nc; i++)
{
bi.insert(i);
}
ans = 0;
while((int) s.size() != 1)
{
Thing tmp = *(s.begin());
s.erase(s.begin());
int i = tmp.i;
int j = tmp.j;
int t = tmp.t;
if(t == 1) RemoveCol(i, j);
else RemoveRow(i, j);
}
cout << ans << "\n";
}
void RemoveCol(int i, int j)
{
bi.erase(j);
set<int>::iterator it = bi.lower_bound(j);
if(it != bi.end())
{
int nx = (*it);
s.erase(Thing(b[nx] - b[j], j, nx, 1));
s.insert(Thing(b[nx] - b[i], i, nx, 1));
}
else
{
int len = j - i;
for(int c = 0; c < len; c++)
{
ans += a.back();
b.pop_back();
}
}
}
void RemoveRow(int i, int j)
{
ai.erase(j);
set<int>::iterator it = ai.lower_bound(j);
if(it != ai.end())
{
int nx = (*it);
s.erase(Thing(a[nx] - a[j], j, nx, 0));
s.insert(Thing(a[nx] - a[i], i, nx, 0));
}
else
{
int len = j - i;
for(int c = 0; c < len; c++)
{
ans += b.back();
a.pop_back();
}
}
}
int modsub(int t1, int t2)
{
int res = t1 - t2;
if(res < 0) res += MOD;
return res;
}
int modadd(int t1, int t2)
{
int res = t1 + t2;
if(res >= MOD) res -= MOD;
return res;
}
int modmul(int t1, int t2)
{
int64_t res = 1LL * t1 * t2;
return (res % MOD);
}
bool BIT(int& mask, int& j)
{
return (mask & MASK(j));
}
int MASK(int j)
{
return (1 << j);
}
};
void __startup()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
}
int main()
{
__startup();
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
Solution().solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
236 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
560 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
155 ms |
9808 KB |
Output is correct |
5 |
Correct |
89 ms |
9176 KB |
Output is correct |
6 |
Correct |
43 ms |
4136 KB |
Output is correct |
7 |
Correct |
579 ms |
23800 KB |
Output is correct |
8 |
Correct |
591 ms |
23804 KB |
Output is correct |
9 |
Correct |
604 ms |
24576 KB |
Output is correct |
10 |
Correct |
595 ms |
24544 KB |
Output is correct |
11 |
Correct |
164 ms |
24432 KB |
Output is correct |
12 |
Correct |
580 ms |
24544 KB |
Output is correct |
13 |
Correct |
577 ms |
24640 KB |
Output is correct |
14 |
Correct |
318 ms |
24652 KB |
Output is correct |
15 |
Correct |
123 ms |
24748 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
0 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
324 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
236 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
560 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
155 ms |
9808 KB |
Output is correct |
33 |
Correct |
89 ms |
9176 KB |
Output is correct |
34 |
Correct |
43 ms |
4136 KB |
Output is correct |
35 |
Correct |
579 ms |
23800 KB |
Output is correct |
36 |
Correct |
591 ms |
23804 KB |
Output is correct |
37 |
Correct |
604 ms |
24576 KB |
Output is correct |
38 |
Correct |
595 ms |
24544 KB |
Output is correct |
39 |
Correct |
164 ms |
24432 KB |
Output is correct |
40 |
Correct |
580 ms |
24544 KB |
Output is correct |
41 |
Correct |
577 ms |
24640 KB |
Output is correct |
42 |
Correct |
318 ms |
24652 KB |
Output is correct |
43 |
Correct |
123 ms |
24748 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
320 KB |
Output is correct |
47 |
Correct |
0 ms |
316 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
324 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
177 ms |
11468 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
10 ms |
1464 KB |
Output is correct |
58 |
Correct |
36 ms |
3668 KB |
Output is correct |
59 |
Correct |
604 ms |
25832 KB |
Output is correct |
60 |
Correct |
592 ms |
25688 KB |
Output is correct |
61 |
Correct |
619 ms |
25752 KB |
Output is correct |
62 |
Correct |
565 ms |
25748 KB |
Output is correct |
63 |
Correct |
129 ms |
25628 KB |
Output is correct |
64 |
Correct |
605 ms |
25760 KB |
Output is correct |
65 |
Correct |
599 ms |
25760 KB |
Output is correct |
66 |
Correct |
190 ms |
25688 KB |
Output is correct |
67 |
Correct |
138 ms |
25964 KB |
Output is correct |