1809 auto cows = map<unsigned int, int>();
1810 auto indexes = vector<unsigned int>();
1811 auto sum = unordered_map<int, set<unsigned int>>();
1812 for(
unsigned int i = 0; i < n; i++) {
1816 indexes.push_back(x);
1818 cows.insert(pair(x, 1));
1820 cows.insert(pair(x, -1));
1823 sort(indexes.begin(), indexes.end());
1825 unsigned int g_count = 0;
1826 unsigned int h_count = 0;
1827 unsigned int g_max = 0;
1828 unsigned int h_max = 0;
1832 for(
auto &cow: cows) {
1833 if(cow.second == 1) {
1838 g_start = cow.first;
1840 g_count = cow.first - g_start;
1842 g_max = max(g_max, g_count);
1848 h_start = cow.first;
1850 h_count = cow.first - h_start;
1852 h_max = max(h_max, h_count);
1854 count += cow.second;
1856 if(!sum.contains(cow.second)) {
1857 auto s = set<unsigned int>();
1858 s.insert(cow.first);
1859 sum.insert(pair(cow.second, s));
1861 sum[cow.second].insert(cow.first);
1864 unsigned int maximum = 0;
1865 for(
const auto &i: sum) {
1867 maximum = max(maximum, *i.second.rbegin() - indexes[0]);
1868 }
else if(i.second.size() >= 2) {
1869 maximum = max(maximum, *i.second.rbegin() - *upper_bound(indexes.begin(), indexes.end(), *i.second.begin()));
1872 cout << max(maximum, max(g_max, h_max));