TASK MASTERS - SOLUTION
The task was solved using vectors. For sorting, the sort function from the header was used
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n,in,z=0;
vector<int>kv; /*A vector containing the values of the number of nails per box*/
cin>>n; /*Number of boxes*/
for(int i=0;i<n;i++){
cin>>in;
kv.push_back(in); /*Adds an element to the end of a vector*/
}
// cout<<"Sorted:"<<endl;
sort(kv.begin(),kv.end()); /*Sorts the vector in ascending order*/
for(int i=0;i<2;i++){
z +=kv[i]; /*Adds the first two, smallest elements of the vector*/
}
cout<<z<<endl;
return 0;
}
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n,in,z=0;
vector<int>kv; /*A vector containing the values of the number of nails per box*/
cin>>n; /*Number of boxes*/
for(int i=0;i<n;i++){
cin>>in;
kv.push_back(in); /*Adds an element to the end of a vector*/
}
// cout<<"Sorted:"<<endl;
sort(kv.begin(),kv.end()); /*Sorts the vector in ascending order*/
for(int i=0;i<2;i++){
z +=kv[i]; /*Adds the first two, smallest elements of the vector*/
}
cout<<z<<endl;
return 0;
}
Return to the web page Preparation for district competitions