1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <vector>
#include <map>

#ifdef DEBUG
#define DEBUG_LOG(x) do { \
	std::cerr << x << std::endl;\
} while (0)
#else
#define DEBUG_LOG(x) do { \
} while (0)
#endif

using NumMap_t = std::map<uint64_t, uint64_t>;

using NumMapIt_t=NumMap_t::iterator;

std::ostream& operator<<(std::ostream& s, NumMapIt_t& it)
{
	s << "{" << it->first << ", " << it->second << "}";
	return s;
}

std::ostream& operator<<(std::ostream& s, NumMap_t& m)
{
	s << "{\n";
	for (auto const& el : m)
	{
		s << "{ " << el.first;
		s << " : " << el.second << " }\n";
	}
	s << " }";
	return s;
}


NumMapIt_t GetFirstProperSum(NumMap_t& sumMap, uint64_t val)
{
	auto inserted = sumMap.emplace(val - 1, 0);
	NumMapIt_t it{};
	if (inserted.second)
	{
		it = std::next(inserted.first);
		sumMap.erase(inserted.first);
	}
	else 
	{
		it = inserted.first;
	}
	return it;
}

void MergeMaps(NumMap_t& m1, NumMap_t& m2)
{
	for(auto const & el : m2)
	{
		auto ins = m1.emplace(el.first, el.second);
		if (!ins.second)
		{
			ins.first->second += el.second;
		}
	}
}

uint64_t UpdateSums(NumMap_t& m, NumMapIt_t& start, uint64_t val)
{
	NumMap_t temp{};
	uint64_t totalNewSets = 0;
	auto it = start;
	while(it != m.end())
	{
		uint64_t newSum = it->first + val;
		uint64_t newSumCnt = it->second;
		auto inserted = temp.emplace(newSum, newSumCnt);
		if (!inserted.second)
		{
			inserted.first->second += newSumCnt;
		}
		totalNewSets += newSumCnt;
		++it;
	}
	MergeMaps(m, temp);
	return totalNewSets;
}


void ReadInput(uint64_t num, std::istream& in, NumMap_t& m)
{
	uint64_t val;
	for (uint64_t i = 0; i < num; ++i)
	{
		in >> val;
		auto it = m.find(val);
		if (it == m.end())
		{
			m.emplace(val, 1);
		}
		else
		{
			++m[val];
		}
	}
}

void UpdateTotal(uint64_t& val, uint64_t add)
{
	uint64_t const k_max = 1000000007;
	val += add;
	if (val > k_max)
	{
		val -= k_max;
	}
}

uint64_t Process(NumMap_t& in, NumMap_t& sums)
{
	uint64_t total = 0;
	for (auto const& el : in)
	{
		for (uint64_t i = 0; i < el.second; ++i)
		{
			auto it = GetFirstProperSum(sums, el.first);
			if (it == sums.end())
			{
				return total;
			}
			UpdateTotal(total, UpdateSums(sums, it, el.first));
		}
	}
	return total;
}

void Run(std::istream& input, std::ostream& output)
{
	uint64_t num;
	input >> num;
	NumMap_t m{};
	NumMap_t sumMap{{0,1}};
	ReadInput(num, input, m);
	auto val = Process(m, sumMap);
	output << val << std::endl;
	DEBUG_LOG(m);
}


#ifndef DEBUG
int main(int argc, char* argv[])
#else
int main_(int argc, char* argv[])
#endif
{
	::Run(std::cin, std::cout);
	return 0;
}