求子集重量之和(Calculate the sum of a subset’s weight)
时限:100ms 内存限制:10000K 总时限:1000ms
描述:
现有n件物品,已知它们的重量,求其中一个子集的重量之和。
There are n things, we are known their weight, calculate the sum of a subset’s weight.
输入:
先输入一个整数n,再输入n件物品的重量,最后输入n个元素表示子集(第i个元素为0,表示子集中不包含该物品,第i个元素为1,表示子集中包含该物品)。
Input a positive integer n first, and then Input the weight of n items, at last we input the n numbers indicate the subset (i-th element is 0, which means that the subset does not contain the i-th item, the i-th element is 1, which means that the subset contains the i-th item).
输出:
输出该子集的重量之和。
Output the sum of subset’s weight.
输入样例:
5
2 9 8 7 5
0 1 1 0 1
输出样例:
22
代码:
#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
typedef struct node{
int index;
int weight;
int flag;
}node;
int main()
{
int sum;
cin>>sum;
vector<node>v;
for(int i=0;i<sum;i++){
int w;
cin>>w;
node n;
n.index=i;
n.weight=w;
v.push_back(n);
}
for(int i=0;i<sum;i++){
cin>>v[i].flag;
}
int answer=0;
for(vector<node>::iterator it=v.begin();it!=v.end();it++){
if((*it).flag==1){
answer+=(*it).weight;
}
}
cout<<answer<<endl;
return 0;
}